zoukankan      html  css  js  c++  java
  • 字符串模板

    kmp

    //Achen
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<vector>
    #include<cstdio>
    #include<queue>
    #include<cmath>
    #include<set>
    #define For(i,a,b) for(int i=(a);i<=(b);i++)
    #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
    const int N=1000007;
    typedef long long LL; 
    using namespace std;
    int n,m,nxt[N],ans[N];
    char s1[N],s2[N];
    
    template<typename T> void read(T &x) {
        char ch=getchar(); x=0; T f=1;
        while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
        if(ch=='-') f=-1,ch=getchar();
        for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
    }
    
    void make_nxt(char s[],int n) {
        for(int i=1,k=0;i<n;i++) {
            while(k&&s[i]!=s[k]) k=nxt[k-1];
            if(s[i]==s[k]) k++;
            nxt[i]=k;
        }
    }
    
    void qry(char s[],int n,char p[],int m) {
        for(int i=0,k=0;i<n;i++) {
            while(k&&s[i]!=p[k]) k=nxt[k-1];
            if(s[i]==p[k]) k++;
            if(k==m) ans[++ans[0]]=i-m+2;
        }
    }
    
    int main() {
    #ifdef DEBUG
        freopen(".in","r",stdin);
        freopen(".out","w",stdout);
    #endif
        scanf("%s",s1);
        scanf("%s",s2);
        n=strlen(s1); 
        m=strlen(s2);
        make_nxt(s2,m);
        qry(s1,n,s2,m);
        For(i,1,ans[0]) printf("%d
    ",ans[i]);
        For(i,0,m-1) printf("%d ",nxt[i]); puts("");
        return 0;
    }
    /*
    ABABABC
    ABA
    */
    View Code

     

    AC自动机

    简单版

     1 //Achen
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cstring>
     5 #include<cstdlib>
     6 #include<vector>
     7 #include<cstdio>
     8 #include<queue>
     9 #include<cmath>
    10 #include<set>
    11 #define For(i,a,b) for(int i=(a);i<=(b);i++)
    12 #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
    13 const int N=1e6+7;
    14 typedef long long LL; 
    15 using namespace std;
    16 int n,ans;
    17 char s[N];
    18 
    19 template<typename T> void read(T &x) {
    20     char ch=getchar(); x=0; T f=1;
    21     while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
    22     if(ch=='-') f=-1,ch=getchar();
    23     for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
    24 }
    25 
    26 int ch[N][26],fail[N],w[N],rt,tot;
    27 void insert() {
    28     if(!rt) rt=++tot;
    29     int len=strlen(s),x=rt;
    30     For(i,0,len-1) {
    31         int c=s[i]-'a';
    32         if(!ch[x][c]) ch[x][c]=++tot;
    33         x=ch[x][c];
    34     }
    35     w[x]++;
    36 }
    37 
    38 queue<int>que;
    39 void get_fail() {
    40     que.push(rt);
    41     while(!que.empty()) {
    42         int x=que.front();
    43         que.pop();
    44         For(i,0,25) if(ch[x][i]) {
    45             int y=ch[x][i];
    46             if(x==rt) fail[y]=rt;
    47             else {
    48                 int z=fail[x];
    49                 while(fail[z]&&!ch[z][i]) z=fail[z];
    50                 if(ch[z][i]) fail[y]=ch[z][i];
    51                 else fail[y]=rt;
    52             }
    53             que.push(y);
    54         }
    55     }
    56 }
    57 
    58 void qry() {
    59     int len=strlen(s),x=rt;
    60     For(i,0,len-1) {
    61         int c=s[i]-'a';
    62         while(!ch[x][c]&&fail[x]) x=fail[x];
    63         if(ch[x][c]) {
    64             x=ch[x][c];
    65             if(w[x]) ans+=w[x];
    66             w[x]=0;
    67             int y=fail[x];
    68             while(y&&w[y]) {
    69                 ans+=w[y];
    70                 w[y]=0;
    71                 y=fail[y];
    72             }
    73         }
    74     }
    75 }
    76 
    77 int main() {
    78 #ifdef DEBUG
    79     freopen(".in","r",stdin);
    80     freopen(".out","w",stdout);
    81 #endif
    82     read(n);
    83     For(i,1,n) {
    84         scanf("%s",s);
    85         insert();
    86     }
    87     get_fail();
    88     scanf("%s",s);
    89     qry();
    90     printf("%d
    ",ans);
    91     return 0;
    92 }
    93 /*
    94 2
    95 a
    96 aa
    97 aa
    98 */
    View Code

    加强版

      1 //Achen
      2 #include<algorithm>
      3 #include<iostream>
      4 #include<cstring>
      5 #include<cstdlib>
      6 #include<vector>
      7 #include<cstdio>
      8 #include<queue>
      9 #include<cmath>
     10 #include<set>
     11 #define For(i,a,b) for(int i=(a);i<=(b);i++)
     12 #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
     13 const int N=1e6+7;
     14 typedef long long LL; 
     15 using namespace std;
     16 int n,ansmx,ans[N],tid[157];
     17 char s[N],ss[157][77];
     18 
     19 template<typename T> void read(T &x) {
     20     char ch=getchar(); x=0; T f=1;
     21     while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
     22     if(ch=='-') f=-1,ch=getchar();
     23     for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
     24 }
     25 
     26 int ch[N][26],fail[N],rt,tot;
     27 void insert(int id) {
     28     if(!rt) rt=++tot;
     29     int len=strlen(ss[id]),x=rt;
     30     For(i,0,len-1) {
     31         int c=ss[id][i]-'a';
     32         if(!ch[x][c]) ch[x][c]=++tot;
     33         x=ch[x][c];
     34     }
     35     tid[id]=x;
     36 }
     37 
     38 queue<int>que;
     39 int sta[N],top;
     40 void get_fail() {
     41     que.push(rt);
     42     while(!que.empty()) {
     43         int x=que.front();
     44         que.pop();
     45         For(i,0,25) if(ch[x][i]) {
     46             int y=ch[x][i];
     47             if(x==rt) fail[y]=rt;
     48             else {
     49                 int z=fail[x];
     50                 while(fail[z]&&!ch[z][i]) z=fail[z];
     51                 if(ch[z][i]) fail[y]=ch[z][i];
     52                 else fail[y]=rt;
     53             }
     54             que.push(y);
     55             sta[++top]=y;
     56         }
     57     }
     58 }
     59 
     60 int cnt[N];
     61 void qry() {
     62     int len=strlen(s),x=rt;
     63     For(i,0,len-1) {
     64         int c=s[i]-'a';
     65         while(!ch[x][c]&&fail[x]) x=fail[x];
     66         if(ch[x][c]) {
     67             x=ch[x][c];
     68             cnt[x]++;
     69         }
     70     }
     71     while(top) {
     72         int x=sta[top--];
     73         cnt[fail[x]]+=cnt[x];
     74     }
     75     ansmx=0; ans[0]=0;
     76     For(i,1,n) ansmx=max(ansmx,cnt[tid[i]]); 
     77     For(i,1,n) if(ansmx==cnt[tid[i]]) ans[++ans[0]]=i;
     78     printf("%d
    ",ansmx);
     79     For(i,1,ans[0]) puts(ss[ans[i]]);
     80 }
     81 
     82 int main() {
     83 #ifdef DEBUG
     84     freopen(".in","r",stdin);
     85     freopen(".out","w",stdout);
     86 #endif
     87     while(scanf("%d",&n)) {
     88         if(!n) break;
     89         memset(ch,0,sizeof(ch));
     90         memset(cnt,0,sizeof(cnt));
     91         tot=rt=top=0;
     92         For(i,1,n) {
     93             scanf("%s",ss[i]);
     94             insert(i);
     95         }
     96         get_fail();
     97         scanf("%s",s);
     98         qry();
     99     }
    100     return 0;
    101 }
    102 /*
    103 2
    104 aba
    105 bab
    106 ababababac
    107 6
    108 beta
    109 alpha
    110 haha
    111 delta
    112 dede
    113 tata
    114 dedeltalphahahahototatalpha
    115 0
    116 */
    View Code

     

    manacher

    //Achen
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<vector>
    #include<cstdio>
    #include<queue>
    #include<cmath>
    #include<set>
    #define For(i,a,b) for(int i=(a);i<=(b);i++)
    #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
    const int N=21000007;
    typedef long long LL; 
    typedef double db;
    using namespace std;
    char ss[N],s[N];
    int len,n,rad[N],ans;
    
    template<typename T> void read(T &x) {
        char ch=getchar(); x=0; T f=1;
        while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
        if(ch=='-') f=-1,ch=getchar();
        for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
    }
    
    void manacher(char s[],int n) {
        for(int i=1,k=0,j;i<n;) {
            while(s[i-k-1]==s[i+k+1]) k++;
            rad[i]=k; ans=max(ans,k);
            for(j=1;j<=k&&rad[i-j]!=rad[i]-j;j++) 
                rad[i+j]=min(rad[i-j],rad[i]-j);
            i+=j;
            k=max(0,k-j);
        }
    }
    
    int main() {
    #ifdef DEBUG
        freopen(".in","r",stdin);
        freopen(".out","w",stdout);
    #endif
        scanf("%s",ss);
        len=strlen(ss);
        s[n++]='*';
        For(i,0,len) {
            s[n++]='#';
            s[n++]=ss[i];
        } 
        s[n++]='#';
        s[n++]='&';
        manacher(s,n);
        printf("%d
    ",ans);
        return 0;
    }
    View Code

     

    后缀数组

     1 //Achen
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cstring>
     5 #include<cstdlib>
     6 #include<vector>
     7 #include<cstdio>
     8 #include<queue>
     9 #include<cmath>
    10 #include<set>
    11 #define For(i,a,b) for(int i=(a);i<=(b);i++)
    12 #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
    13 const int N=2e6+7;
    14 typedef long long LL; 
    15 typedef double db;
    16 using namespace std;
    17 int n,sa[N],rak[N],h[N];
    18 char s[N];
    19 
    20 template<typename T> void read(T &x) {
    21     char ch=getchar(); x=0; T f=1;
    22     while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
    23     if(ch=='-') f=-1,ch=getchar();
    24     for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
    25 }
    26 
    27 void make_hight() {
    28     For(i,0,n-1) rak[sa[i]]=i;
    29     for(int i=0,k=0;i<n;i++) {
    30         if(!rak[i]) continue;
    31         if(k) k--;
    32         int j=sa[rak[i]-1];
    33         while(s[i+k]==s[j+k]) k++;
    34         h[rak[i]-1]=k;
    35     }
    36 }
    37 
    38 int cmp(int a,int b,int k,int y[]) {
    39     int o1=a+k>=n?-1:y[a+k];
    40     int o2=b+k>=n?-1:y[b+k];
    41     return o1==o2&&y[a]==y[b];
    42 }
    43 
    44 void make_sa() {
    45     static int t1[N],t2[N],c[N];
    46     int *x=t1,*y=t2,p,m='z'+1;
    47     For(i,0,m-1) c[i]=0;
    48     For(i,0,n-1) c[x[i]=s[i]]++;
    49     For(i,1,m-1) c[i]+=c[i-1];
    50     Rep(i,n-1,0) sa[--c[x[i]]]=i;
    51     for(int k=1;k<=n;k<<=1) {
    52         p=0;
    53         For(i,n-k,n-1) y[p++]=i;
    54         For(i,0,n-1) if(sa[i]>=k) y[p++]=sa[i]-k;
    55         For(i,0,m-1) c[i]=0;
    56         For(i,0,n-1) c[x[y[i]]]++;
    57         For(i,1,m-1) c[i]+=c[i-1];
    58         Rep(i,n-1,0) sa[--c[x[y[i]]]]=y[i];
    59         swap(x,y); x[sa[0]]=0; p=1;
    60         For(i,1,n-1) 
    61             x[sa[i]]=cmp(sa[i],sa[i-1],k,y)?p-1:p++;
    62         if(p>=n) break;
    63         m=p;
    64     }
    65     make_hight();
    66 }
    67 
    68 int main() {
    69 #ifdef DEBUG
    70     freopen(".in","r",stdin);
    71     freopen(".out","w",stdout);
    72 #endif
    73     scanf("%s",s);
    74     n=strlen(s);
    75     make_sa();
    76     For(i,0,n-1) printf("%d ",sa[i]+1);
    77     return 0;
    78 }
    View Code

     

    后缀自动机

    //Achen
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdlib>
    #include<vector>
    #include<cstdio>
    #include<queue>
    #include<cmath>
    #include<set>
    #define For(i,a,b) for(int i=(a);i<=(b);i++)
    #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
    const int N=2e6+7;
    typedef long long LL; 
    typedef double db;
    using namespace std;
    int n,rt,fa[N],sz[N],l[N],ch[N][26],p,np,last,tot;
    LL ans;
    char s[N];
    
    template<typename T> void read(T &x) {
        char ch=getchar(); x=0; T f=1;
        while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
        if(ch=='-') f=-1,ch=getchar();
        for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
    }
    
    void insert(int c) {
        if(!rt) { rt=++tot; last=rt; }
        p=last; np=++tot; last=np;
        l[np]=l[p]+1; sz[np]++;
        for(;p&&!ch[p][c];p=fa[p]) ch[p][c]=np;
        if(!p) fa[np]=rt;
        else {
            int q=ch[p][c];
            if(l[q]==l[p]+1) fa[np]=q;
            else {
                int nq=++tot; l[nq]=l[p]+1;
                memcpy(ch[nq],ch[q],sizeof(ch[q]));
                fa[nq]=fa[q]; fa[q]=fa[np]=nq;
                for(;p&&ch[p][c]==q;p=fa[p]) ch[p][c]=nq;
            } 
        }
    }
    
    void solve() {
        static int sa[N],c[N];
        For(i,1,tot) c[l[i]]++;
        For(i,1,n) c[i]+=c[i-1];
        For(i,1,tot) sa[c[l[i]]--]=i;
        Rep(i,tot,1) {
            int x=sa[i];
            sz[fa[x]]+=sz[x];
            if(sz[x]>1) ans=max(ans,(LL)sz[x]*l[x]);
        }
    }
    
    int main() {
    #ifdef DEBUG
        freopen(".in","r",stdin);
        freopen(".out","w",stdout);
    #endif
        scanf("%s",s);
        n=strlen(s);
        For(i,0,n-1) insert(s[i]-'a');
        solve();
        printf("%lld
    ",ans);
        return 0;
    }
    View Code

     

    回文自动机

     1 //Achen
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cstring>
     5 #include<cstdlib>
     6 #include<vector>
     7 #include<cstdio>
     8 #include<queue>
     9 #include<cmath>
    10 #include<set>
    11 #define For(i,a,b) for(int i=(a);i<=(b);i++)
    12 #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
    13 const int N=1e5+7; 
    14 typedef long long LL;
    15 typedef double db;
    16 using namespace std;
    17 char s[N];
    18 int ans,n,last,tot,rt1,rt2,ch[N][26],len[N],fail[N],l[N],r[N],sz[N];
    19 
    20 void init() {
    21     tot=0; len[rt1=0]=0; len[rt2=++tot]=-1;
    22     fail[rt1]=rt2; last=rt1;
    23       memset(ch,0,sizeof(ch));
    24 }
    25 
    26 void insert(int n,int c,int p[]) {
    27     int x=last,y;
    28     for(;s[n]!=s[n-len[x]-1];x=fail[x]);
    29     if(!ch[x][c]) {
    30         len[++tot]=len[x]+2;
    31         for(y=fail[x];s[n]!=s[n-len[y]-1];y=fail[y]);
    32         fail[tot]=ch[y][c];
    33         ch[x][c]=tot;
    34     }
    35     last=ch[x][c];
    36     sz[last]++;
    37     p[n]=len[last];
    38 }
    39 
    40 int main() {
    41 #ifdef DEBUG
    42     freopen(".in","r",stdin);
    43     freopen(".out","w",stdout);
    44 #endif
    45     scanf("%s",s);
    46     n=strlen(s);  
    47     init();
    48     for(int i=0;i<n;i++) 
    49         insert(i,s[i]-'a',l);
    50     for(int i=0;i<=n/2;i++) swap(s[i],s[n-i-1]);
    51     init();
    52     for(int i=0;i<n;i++) insert(i,s[i]-'a',r);
    53     for(int i=0;i<n-1;i++) 
    54         ans=max(ans,r[i]+l[n-(i+1)-1]);
    55     printf("%d
    ",ans);
    56     return 0;
    57 }
    View Code

     

  • 相关阅读:
    程序员的人品
    【转】telnet使用 删除foxmail不能收取的邮件
    35岁以后我在干什么?
    面试
    程序员基本知识数制
    一事无成
    经过XssFilter替换特殊字符后再经zuul路由转发httpEntity缺少内容
    android跳转到市场进行评价 market://search?q
    使用IntelliJ 12.1.12开发android程序
    重定向
  • 原文地址:https://www.cnblogs.com/Achenchen/p/8776309.html
Copyright © 2011-2022 走看看