我的ac自动机模板
2014-07-25 18:08
267 查看
定义:
CH 取数据范围
初始化:
只要清空ch[0],注意sz是从1开始;
插入:
重头戏:
接下来就是询问..
CH 取数据范围
const int NODE = 105 , CH = 4 ,mod = 100000; int ch[NODE][CH], val[NODE] ,sz , fail[NODE];
初始化:
只要清空ch[0],注意sz是从1开始;
void init() { sz=1; memset(ch[0],0,sizeof(ch[0])); }
插入:
void insert(char *s) { int u=0; for(;*s;s++) { int c=idx(*s); if(!ch[u][c]) { memset(ch[sz],0,sizeof(ch[sz])); val[sz]=0; ch[u][c]=sz++; } u=ch[u][c]; } val[u]=1; }
重头戏:
int queue[MAXN]; void getfail() { int *rear=queue,*front=queue; for(int c=0;c<CH;c++) { int u=ch[0][c]; if(u) *rear++=u,f[u]=0,last[u]=0; } while(rear!=front) { int cur = front++; for(int c=0;c<CH;c++) { int &u = ch[cur][c]; int tf=ch[f[cur]][c]; if(!u) { u=tf; continue; } f[u]=tf; last[u]=val[f[u]]?f[u]:last[f[u]]; *rear++=u; } } }
接下来就是询问..
int find(char *s) { int sum=0; int u=0; for(;*s;s++) { int c=idx(*s); u=ch[u][c]; int next=0; if(val[u]) next=u; else if(last[u]) next=last[u]; for(int tmp=next;tmp;tmp=last[tmp]) sum+=val[tmp]; } return sum; }
相关文章推荐
- hdu 2222 ac自动机模板题
- AC自动机模板【洛谷3796】
- hdu 2222 ac自动机模板
- [洛谷3808]【模板】AC自动机(简单版)
- 【洛谷P3796】【模板】AC自动机(加强版)
- 【洛谷】3808 【模板】AC自动机(简单版)
- 洛谷 P3796 【模板】AC自动机(加强版)
- AC自动机模板
- HDU 2825 AC自动机模板
- [hdu2222]ac自动机(模板)
- HDU2896,ac自动机模板题
- AC自动机 模板 hdu 2896
- AC自动机 ( 模板题啊 )——病毒侵袭持续中 ( HDU 3065 )
- ac自动机模板
- AC自动机模板
- HDOJ-2222 AC自动机模板题..注意细节..
- AC自动机 模板与简单讲解 模板题:贴瓷砖
- 【模板练习——AC自动机】Keywords Search HDU - 2222
- 【AC自动机】HDU中模板题
- 洛谷P3796 【模板】AC自动机(加强版)