zoukankan      html  css  js  c++  java
  • 字符串(后缀自动机):Codeforces Round #129 (Div. 1) E.Little Elephant and Strings

    E. Little Elephant and Strings
    time limit per test
    3 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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.

    Examples
    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 
    Note

    Let's assume that you are given string a = a1a2... a|a|, then let's denote the string's length as |a| and the string's i-th character as ai.

    A substring a[l... r] (1 ≤ l ≤ r ≤ |a|) of string a is string alal + 1... ar.

    String a is a substring of string b, if there exists such pair of integers l and r (1 ≤ l ≤ r ≤ |b|), that b[l... r] = a.

      这道题很好啊……

      本来要用后缀数组,现在我用的是后缀自动机。用set维护经过的字串有哪些,建成这棵trie的SAM后,连边fa[i]->i,然后DFS,把set启发式合并一下,要的信息就是size,处理答案时先跳(必定可行),然后size<k就不停跳fa,直到size>=k为止,这时匹配的答案加入计数。

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstring>
     4 #include <cstdio>
     5 #include <set>
     6 using namespace std;
     7 const int N=200010;
     8 int fa[N],ch[N][27],len[N];
     9 string s[N];set<int>t[N];
    10 int tr[N][26],last[N];
    11 int lst,cnt,n,k,tot;
    12 void Insert(int c){
    13     int p=lst,np=lst=++cnt;len[np]=len[p]+1;
    14     while(p&&ch[p][c]==0)ch[p][c]=np,p=fa[p];
    15     if(!p)fa[np]=1;
    16     else{
    17         int q=ch[p][c],nq;
    18         if(len[q]==len[p]+1)
    19             fa[np]=q;
    20         else{
    21             len[nq=++cnt]=len[p]+1;
    22             fa[nq]=fa[q];fa[q]=fa[np]=nq;
    23             memcpy(ch[nq],ch[q],sizeof(ch[q]));
    24             while(ch[p][c]==q)ch[p][c]=nq,p=fa[p];
    25         }    
    26     }
    27 }
    28 int cntE,fir[N],to[N],nxt[N];
    29 void addedge(int a,int b){
    30     nxt[++cntE]=fir[a];
    31     to[fir[a]=cntE]=b;
    32 }
    33 void Initialization(){
    34     memset(fir,0,sizeof(fir));
    35     lst=cnt=1;last[cntE=tot=0]=1;
    36 }
    37 set<int>Merge(set<int>a,set<int>b){
    38     if(a.size()<b.size())swap(a,b);
    39     a.insert(b.begin(),b.end());
    40     delete &b;return a;
    41 }
    42 void DFS(int x){
    43     for(int i=fir[x],g;i;i=nxt[i])
    44         DFS(g=to[i]),t[x]=Merge(t[x],t[g]);
    45 }
    46 
    47 int main(){
    48     Initialization();
    49     scanf("%d%d",&n,&k);
    50     for(int i=1;i<=n;i++){
    51         cin>>s[i];
    52         for(int j=0,p=0,c;j<s[i].size();j++){
    53             c=s[i][j]-'a';lst=last[p];
    54             if(tr[p][c])p=tr[p][c],t[last[p]].insert(i);
    55             else Insert(c),t[last[p=tr[p][c]=++tot]=lst].insert(i);
    56         }
    57     }
    58     for(int i=2;i<=cnt;i++)
    59         addedge(fa[i],i);DFS(1);
    60     for(int i=1;i<=n;i++){
    61         long long ans=0;
    62         for(int j=0,p=1,c,l=0;j<s[i].size();j++){
    63             c=s[i][j]-'a';
    64             p=ch[p][c];l+=1;
    65             while(p!=1&&t[p].size()<k)
    66                 p=fa[p],l=len[p];
    67             ans+=l;    
    68         }
    69         printf("%I64d ",ans);
    70     }
    71     printf("
    ");
    72     return 0;
    73 }

      

  • 相关阅读:
    SSH免密码登录
    Qt编译错误GL/gl.h: No such file or directory
    UVA 11645
    《逆袭大学》文摘——9.4 基础和应用的平衡中找到大学的节奏
    EBS採购模块中的高速接收和高速接收事务
    笔记-Android中打开各种格式的文件(apk、word、excel、ppt、pdf、音视频、图片等)
    git 冲突解决的方法
    SICP 习题 (1.43)解题总结
    Swift百万线程攻破单例(Singleton)模式
    setjmp/longjmp
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5784065.html
Copyright © 2011-2022 走看看