NC205333. WordsFascinating
描述
输入描述
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 (1 ≤ ≤ 5×).
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×.
输出描述
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 .
示例1
输入:
2 aa ab
输出:
8
示例2
输入:
3 abb aca aba
输出:
139
C++11(clang++ 3.9) 解法, 执行用时: 48ms, 内存消耗: 40168K, 提交时间: 2020-06-06 14:55:54
#include<bits/stdc++.h> using namespace std; #define ll long long #define ls o<<1 #define rs o<<1|1 #define pii pair<int,int> #define fi first #define se second #define pb push_back #define mp make_pair const double pi=acos(-1.0); const double eps=1e-10; const int mod=1e9+7; const int INF=0x3f3f3f3f; const int maxn=1e6+5; int n,las,tot,top; string s[maxn]; int dp[maxn<<1],rt[maxn],buf[26]; struct node{ int ch[26]; int fa,len; }a[maxn<<1]; void add(int c){ int p=las,np=las=++tot; a[np].len=a[p].len+1; for(;p&&!a[p].ch[c];p=a[p].fa)a[p].ch[c]=np; if(!p)a[np].fa=top; else{ int q=a[p].ch[c]; if(a[q].len==a[p].len+1)a[np].fa=q; else{ int nq=++tot;a[nq]=a[q]; a[nq].len=a[p].len+1; a[np].fa=a[q].fa=nq; for(;p&&a[p].ch[c]==q;p=a[p].fa)a[p].ch[c]=nq; } } } int dfs(int u){ if(dp[u])return dp[u]; dp[u]=1; for(int i=0;i<26;i++){ if(a[u].ch[i]){ dp[u]=(dp[u]+dfs(a[u].ch[i]))%mod; } } return dp[u]; } int main(void){ // freopen("str1.in","r",stdin); ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); cin>>n; tot=0; for(int i=1;i<=n;i++){ cin>>s[i]; top=rt[i]=las=++tot; for(int j=0;j<s[i].length();j++)add(s[i][j]-'a'); } rt[n+1]=++tot; for(int i=n;i>=1;i--){ for(int j=rt[i];j<rt[i+1];j++){ for(int k=0;k<26;k++){ if(!a[j].ch[k])a[j].ch[k]=buf[k]; } } for(int k=0;k<26;k++){ buf[k]=a[rt[i]].ch[k]; } } cout<<dfs(1)<<endl; return 0; }