Fun with strings

3

2 votes
Arrays, LCP, Medium, String Manipulation, Suffix tree
Problem

You are given a string of length n.
Find the sum of length of LCP(Longest Common Prefix) over all the ordered pairs of its substrings.
Formally, say the substring of S starting at index i and ending at index j is represented by , and let represent the length of LCP of strings A and B.
Then compute the value of :

As this value may be very large, print it modulo 10^9 + 7

Input
The first line contains T, the number of test cases. It is followed by lines, describing the strings in the testcases . Each string is described by two lines.
The first line contains n, length of the string S.
Second line contains the string S consisting of lowercase english alphabets only.

Output
Print T lines containing the sum of lengths of lcp over all ordered pairs of substrings of S modulo 10^9+7 for the T testcases, each on a new line.

Constraints

  • S consists of lowercase english alphabets only.
  • (25 points) :
  • (75 points) :
Sample Input
2
2
ab
3
zzz
Sample Output
6
46
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

When , the answer equals

Editor Image

?