CodeForces-204E:Little Elephant and Strings (後綴自動機)
The Little Elephant loves strings very much.
He has an array a from n strings, consisting of lowercase English letters. Let‘s number the elements of the array from 1 to n, then let‘s denote the element number i as ai. For each string ai (1 ≤ i ≤ n) the Little Elephant wants to find the number of pairs of integers l
Help the Little Elephant solve this problem.
If you are not familiar with the basic notation in string problems, you can find the corresponding definitions in the notes.
Input
The first line contains two space-separated integers — n and k (1 ≤ n, k ≤ 105). Next n lines contain array a. The i-th line contains a non-empty string ai, consisting of lowercase English letter. The total length of all strings ai does not exceed 105
Output
On a single line print n space-separated integers — the i-th number is the answer for string ai.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.
Example
Input3 1Output
abc
a
ab
6 1 3Input
7 4Output
rubik
furik
abab
baba
aaabbbababa
abababababa
zero
1 0 9 9 21 30 0
題意:給定N和L,輸入N個字符串,對於每個字符串,輸出其有多少個字串滿足在這N個串裏出現的次數大於等於K。
思路:廣義後綴自動機模板題,或者後綴數組。
註意:註意K>N時直接輸出若幹個0,不然會超時,GG。
向上傳遞時我大膽的試了一試Bitset,結果超內存了。但是set沒問題。
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int maxn=400010; int N,K,L,times; string s[maxn]; struct SAM { int np,p,nq,q,Last,cnt,cur[maxn],sum[maxn]; int maxlen[maxn],fa[maxn],ch[maxn][26]; SAM(){ Last=cnt=1; } void add(int x){ np=++cnt; p=Last; Last=np; maxlen[np]=maxlen[p]+1; while(p&&!ch[p][x]) ch[p][x]=np,p=fa[p]; if(!p) fa[np]=1; else{ q=ch[p][x]; if(maxlen[q]==maxlen[p]+1) fa[np]=q; else{ nq=++cnt; memcpy(ch[nq],ch[q],sizeof(ch[nq])); cur[nq]=cur[q]; sum[nq]=sum[q]; fa[nq]=fa[q]; fa[np]=fa[q]=nq;maxlen[nq]=maxlen[p]+1; while(p&&ch[p][x]==q) ch[p][x]=nq,p=fa[p]; } } while(np){ if(cur[np]==times||sum[np]>=K) break; sum[np]++; cur[np]=times; np=fa[np]; } } void sort() { } void solve(int now) { int tp=1;long long ans=0; L=s[now].length(); for(int i=0;i<L;i++) { tp=ch[tp][s[now][i]-‘a‘]; int kkk=tp; while(kkk>1){ if(sum[kkk]>=K){ ans+=maxlen[kkk]; break; } kkk=fa[kkk]; } } printf("%lld ",ans); } }sam; int main() { scanf("%d%d",&N,&K); for(int i=1;i<=N;i++){ cin>>s[i]; L=s[i].length(); sam.Last=1; times=i; for(int j=0;j<L;j++) sam.add(s[i][j]-‘a‘); } if(K>N){ for(int i=1;i<=N;i++) printf("0 "); return 0; } sam.sort(); for(int i=1;i<=N;i++) sam.solve(i); return 0; }
拓撲後set傳遞。
#include<set> #include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int maxn=200010; int N,K,times; char s[maxn]; set<int>Set[maxn]; int a[maxn],b[maxn],sum[maxn],L[maxn],R[maxn]; struct SAM { int np,p,nq,q,Last,cnt,maxlen[maxn],fa[maxn],ch[maxn][26]; SAM(){ Last=cnt=1; } void add(int x){ np=++cnt; p=Last; Last=np; maxlen[np]=maxlen[p]+1; while(p&&!ch[p][x]) ch[p][x]=np,p=fa[p]; if(!p) fa[np]=1; else{ q=ch[p][x]; if(maxlen[q]==maxlen[p]+1) fa[np]=q; else{ nq=++cnt; memcpy(ch[nq],ch[q],sizeof(ch[nq])); fa[nq]=fa[q]; fa[np]=fa[q]=nq;maxlen[nq]=maxlen[p]+1; while(p&&ch[p][x]==q) ch[p][x]=nq,p=fa[p]; } } } void sort() { for(int i=1;i<=cnt;i++) a[maxlen[i]]++; for(int i=1;i<=cnt;i++) a[i]+=a[i-1]; for(int i=1;i<=cnt;i++) b[a[maxlen[i]]--]=i; for(int i=1;i<=N;i++){ int tp=1; for(int j=L[i];j<=R[i];j++){ tp=ch[tp][s[j]-‘a‘]; Set[tp].insert(i); } } for(int i=cnt;i>1;i--){ int x=b[i]; sum[x]=Set[x].size(); int y=fa[x]; if(Set[x].size()>Set[y].size()) swap(Set[x],Set[y]); for(set<int>::iterator it=Set[x].begin();it!=Set[x].end();it++) Set[y].insert(*it); } } void solve(int now) { int tp=1; long long ans=0; for(int i=L[now];i<=R[now];i++) { tp=ch[tp][s[i]-‘a‘]; int kkk=tp; while(kkk>1){ if(sum[kkk]>=K){ ans+=maxlen[kkk];break;} kkk=fa[kkk]; } } printf("%lld ",ans); } }sam; int main() { scanf("%d%d",&N,&K); for(int i=1;i<=N;i++){ L[i]=R[i-1]+1; scanf("%s",s+L[i]); R[i]=L[i]+strlen(s+L[i])-1; sam.Last=1; for(int j=L[i];j<=R[i];j++) sam.add(s[j]-‘a‘); } if(K>N){ for(int i=1;i<=N;i++) printf("0 "); return 0; } sam.sort(); for(int i=1;i<=N;i++) sam.solve(i); return 0; }
CodeForces-204E:Little Elephant and Strings (後綴自動機)