CodeForces-204E:Little Elephant and Strings (后缀自动机)

Posted ---学习ing---

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 and r (1 ≤ l ≤ r ≤ |ai|) such that substring ai[l... r] is a substring to at least k strings from array a (including the i-th string).

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

Input
3 1
abc
a
ab
Output
6 1 3 
Input
7 4
rubik
furik
abab
baba
aaabbbababa
abababababa
zero
Output
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 (后缀自动机)的主要内容,如果未能解决你的问题,请参考以下文章

codeforces 204(Div.1 A) Little Elephant and Interval(贪心)

WAV相关:从PCM16 Little Endian数据转WAV文件

@codeforces - 668E@ Little Artem and 2-SAT

The Little Prince-11/29

Little Sub and Traveling(杭师大第十二届校赛E题) 欧拉回路

Little-Endian小字节序