ac自動機系列

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   for   art   

hdu2222這題說的是在一個1000000的長串中找出n個短串是否在其中出現過 最後輸出在長串中出現的個數

#include <iostream>#include <cstdio>#include <cstring>#include <map>#include <queue>#include <string>using namespace std;const int maxn =10000*50+100;const int sign_size = 26;map<string,int>ms;struct Acuth{    int ch[maxn][sign_size];    int val[maxn];    int sz;    int f[maxn];    int last[maxn];    bool use[10005];    void clear(){        memset(ch[0],0,sizeof(ch[0]));        memset(use,false,sizeof(use));        sz=1;        ms.clear();        val[0]=0;    }    int idx(char c){   return c-‘a‘; }    void insert(char *s,int v){        int n = strlen(s), u=0;        for(int i=0; i<n; ++i){            int c=idx(s[i]);            if(ch[u][c]==0){               memset(ch[sz],0,sizeof(ch[sz]));               val[sz]=0;               ch[u][c]=sz++;            }            u=ch[u][c];        }        ms[string(s)]=v;        val[u]=v;    }    void print(int j){        if(j){             use[val[j]] = true;             print(last[j]);        }    }    void find(char *T){         int n = strlen(T);         int j=0;         for(int i=0; i<n; ++i){             int c = idx(T[i]);             while(j&&!ch[j][c]) j=f[j];             j=ch[j][c];             if(val[j]) print(j);             else if(last[j]) print(last[j]);         }    }    void getFail(){        queue<int> q;        f[0]=0;        for(int c = 0; c<sign_size; ++c){              int u = ch[0][c];              if(u){  f[u]=0; q.push(u); last[u]=0;   }        }       while(!q.empty()){          int r= q.front(); q.pop();          for(int c=0; c<sign_size; ++c){              int u=ch[r][c];              if(!u){ ch[r][c] = ch[f[r]][c]; continue; }              q.push(u);              int v = f[r];              while(v&&!ch[v][c]) v=f[v];              f[u]=ch[v][c];              last[u] = val[f[u]]?f[u] : last[f[u]];          }       }    }}ac;char P[10005][55],text[1000005];int main(){    int t;    scanf("%d",&t);    while(t--){       ac.clear();       int n;       scanf("%d",&n);       for(int i=1; i<=n; ++i){            scanf("%s",P[i]);            ac.insert(P[i],i);       }       scanf("%s",text);       ac.getFail();       ac.find(text);       int ans=0;       for(int i=1; i<=n; ++i)        if(ac.use[ms[string(P[i])]]==true) ans++;       printf("%d\n",ans);    }    return 0;}
View Code

的

 

ac自動機系列

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.