zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    chrome新版本flash无法在http网站上运行的解决办法
    SSO单点登录三种情况的实现方式详解(转载)
    解决请求筛选模块被配置为拒绝包含的查询字符串过长的请求(原创)
    更改mysql默认字符集 (转载)
    用TextWriterTraceListener实现log文件记录 (转载)
    PO BO VO DTO POJO DAO概念及其作用
    SqlServer 的一个坑
    关于windows服务注册的问题
    原生js的数组除重复
    ES6 箭头函数下的this指向和普通函数的this对比
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8593182.html
Copyright © 2011-2022 走看看