AC自动机模板
2013-09-23 19:47
316 查看
struct AhoCorasickAutomata { int ch[maxnode][sigma_size],last[maxnode]; int fail[maxnode],val[maxnode]; int sz; void init() { memset(ch[0],0,sizeof(ch[0])); sz=1; } int idx(char c){return c-'a';} void insert(char *s,int v) { int u=0,n=strlen(s); for(int i=0;i<n;i++) { int c=idx(s[i]); if(!ch[u][c]) { memset(ch[sz],0,sizeof(ch[sz])); val[sz]=0; ch[u][c]=sz++; } u=ch[u][c]; } val[u]=v; } void getfail() { queue<int>q; fail[0]=0; for(int c=0;c<sigma_size;c++) { int u=ch[0][c]; if(u){ fail[u]=0;q.push(u);last[u]=0;} } while(!q.empty()) { int r=q.front();q.pop(); for(int c=0;c<sigma_size;c++) { int u=ch[r][c]; if(!u){ch[r][c]=ch[fail[r]][c];continue;} q.push(u); fail[u]=ch[fail[r]][c]; last[u] = val[fail[u]]?fail[u]:last[fail[u]]; } } } void print(int j) { if(j) { printf("%d\n",val[j]); 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]); j=ch[j][c]; if(val[j]) print(j); else if(last[j]) print(last[j]); } } };
相关文章推荐
- HDU 2825 AC自动机模板
- AC自动机模板(数组+指针)hdu2222
- AC自动机模板
- AC自动机-HDU2222-模板题
- AC自动机 模板 hdu 2896
- AC自动机模板
- HDOJ-2222 AC自动机模板题..注意细节..
- AC自动机模板
- AC自动机模板
- AC自动机模板
- Keywords Search - HDU 2222(AC自动机模板)
- AC自动机模板(【CJOJ1435】)
- HDU 2222 AC自动机模板
- 洛谷.3808/3796.[模板]AC自动机
- AC自动机模板
- HDOJ 2222 - Keywords Search 更新AC自动机模板...用类来表示
- cjoj P1435 - 【模板题 USACO】AC自动机 && 洛谷 P3796 【模板】AC自动机(加强版)
- 字典树模板/ac自动机模板
- HDU 3065 病毒侵袭持续中(AC自动机 数组模板)
- AC自动机-HDU2896-模板题