AC自动机模板
2015-09-03 18:36
337 查看
struct ACauto{ int ch[maxn][26]; int sz; int f[maxn],last[maxn],val[maxn],cnt[maxn]; void init(){ sz=1; memset(ch[0],0,sizeof ch[0]); memset(cnt,0,sizeof cnt); } int idx(char c){ return c-'A'; } void add(char *s,int v){ int u=0,len=strlen(s); for(int i=0;i<len;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; f[0]=0; for(int c=0;c<26;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<26;c++){ int u=ch[r][c]; if(!u){ ch[r][c]=ch[f[r]][c]; continue; } q.push(u); f[u]=ch[f[r]][c]; last[u]=val[f[u]]?f[u]:last[f[u]]; } } } void print(int j){ if(j){ cnt[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]); while(j&&!ch[j][c])j=f[j]; j=ch[j][c]; if(val[j])print(j); else if(last[j])print(last[j]); } } }ac;
相关文章推荐
- sql server 2012 报表开发(5) reporting service 动态显示和隐藏列
- 分析手动构建复杂的Cell—(新浪为例)
- SwipeListView 详解 实现微信,QQ等滑动删除效果
- Effective C++——条款17(第3章)
- Java记录 -22- Java的基类Object详解
- A new friend,Tim
- 没有拥有,只有当下
- 学习笔记1
- 【polya】 HDOJ Count the Tetris
- LNK2005错误 :error LNK2005: 已经在 libcmtd.lib(dbgheap.obj) 中定义
- 2012-2013 ACM-ICPC, NEERC, Central Subregional Contest J Computer Network1 (缩点+最远点对)
- Thymeleaf模板引擎 学习笔记
- XMPP的定义及简单介绍
- 【JavaScript中的正则表达式】
- What does “%.*s” mean in printf?
- C++基础---string类的clear/erase/pop_back
- hdu~4282(数学题)
- Access-based enumeration in Windows Server 2012
- Java基础-内部类及其应用
- 不用堆栈实现树的先序遍历