As we all know the fact, you can use several Source Words to compose an Interesting Word. Little Gyro has already learnt this fantastic idea from one of his best friends Brother Yu.
On Little Gryo's birthday, fortunately, Little Gryo received a gift from Brother Yu. The gift consists of n Interesting Words . Little Gyro also thought those words were really fantastic. So he decided to play with these words.
For each Interesting Word , Little Gyro will select a period of consecutive letters , and splice into a new word T within the given order, and then he defined these kind of words "Fascinating Words". It means you can take apart a Fascinating Word T and form n period of consecutive letters from the given Interesting Words. Specially, if Little Gyro considers the Interesting Word really useless, he'll not choose any letter from this Interesting Word either. For example, supposed that there are some Interesting Words: . Little Gyro may select a period of consecutive letters from each given word such as: . And then form the Fascinating Word . Specially, Little Gyro also can only selectand form the Fascinating Word , if he dislikes the first Interesting Word.
Now given all the Interesting Words that Little Gyro has received on his birthday, Little Gyro wants to know the total number of the different Fascinating Words that he can generate.
Input Specification:
Each input file only contains one test case.
The first line contains an integer n (1 ≤ n ≤ 2000), indicating the number of the Interesting Words.
Then, the following n lines, each line contains an Interesting Word .
It's guaranteed that the Interesting Words can only be made up by the lowercase letters, and the sum of the length of the Interesting Words of all test cases will not exceed 5×104.
Output Specification:
For each test case, output the number of the different Fascinating Words that Little Gyro can generate.
Because the number may be very large, just output the number mod .
Sample Input 1:
2
aa
ab
结尾无空行
Sample Output 1:
8
结尾无空行
Sample Input 2:
3
abb
aca
aba
结尾无空行
Sample Output 2:
139
结尾无空行
Hint:
For the first sample, Little Gyro can generate 8 different Fascinating Words in total, including "a", "b", "aa", "ab", "aaa", "aab", "aaab" and the empty word.
代码:
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1000100;
const int mod=1e9+7;
char str[maxn];
int dp[26];
int f[maxn];
string s[2010];
struct Sam
{
int last,cnt;
int nt[maxn<<1][26],fa[maxn<<1];
int len[maxn<<1];
int sum[maxn<<1];
int x[maxn<<1],y[maxn<<1];
void init(int n)
{
last=1;
cnt=1;
fa[1]=0;
len[1]=0;
for(int i=0;i<=n;i++)
{
fa[i]=0;
len[i]=0;
sum[i]=0;
x[i]=0;
memset(nt[i],0,sizeof(nt[i]));
}
}
void _insert(int c)
{
int i=++cnt,p=last;
len[i]= len[last] + 1;
while(p&&!nt[p][c]) nt[p][c]=i, p=fa[p];
if(!p) fa[i]=1;
else
{
int q=nt[p][c];
if(len[q]==len[p]+1) fa[i]=q;
else
{
int j=++cnt;
len[j]= len[p] + 1;
memcpy(nt[j], nt[q], sizeof(nt[q]));
fa[j]=fa[q];
fa[i]= fa[q]=j;
while(p&&nt[p][c]==q) nt[p][c]=j, p=fa[p];
}
}
last=i;
sum[last]=1;
}
void _count()
{
for(int i=1;i<=cnt;i++) x[len[i]]++;
for(int i=1;i<=cnt;i++) x[i]+=x[i-1];
for(int i=1;i<=cnt;i++) y[x[len[i]]--]=i;
for(int i=cnt;i>=1;i--)
{
int now=y[i];
f[now]=1;
for(int j=0;j<26;j++)
{
if(nt[now][j]) f[now]=(f[now]+f[nt[now][j]])%mod;
else f[now]=(f[now]+dp[j])%mod;
}
}
for(int i=0;i<26;i++)
if(nt[1][i]) dp[i]=f[nt[1][i]];
}
void creat(int q)
{
int len;
len = s[q].size();
init(len*2+10);
for(int i=0;i<len;i++)
_insert(s[q][i]-'a');
_count();
}
}sam;
int main()
{
int n;
cin >> n;
for(int i=1;i<=n;i++)
{
cin >> str;
s[i]=str;
}
for(int i=n;i>=1;i--)
sam.creat(i);
int ans=0;
for(int i : dp)
ans=(ans+i)%mod;
cout << ans+1 <<endl;
return 0;
}