hdu2222Keywords Search
来源:互联网 发布:成都多益网络 编辑:程序博客网 时间:2024/06/05 02:52
#include <iostream>#include <cstring>#include <cstdio>#include <queue>using namespace std;const int N=1000000+5;struct node{ int son[26],fail,sum; bool vis;}trie[N];int tot,ans;void init(){ for(int j=0;j<=tot;j++){ trie[j].fail=trie[j].sum=trie[j].vis=0; for(int i=0;i<26;i++) trie[j].son[i]=0; } /* memset(trie,0,sizeof(trie));//这种方法爆内存 trie[0].fail=trie[0].sum=trie[0].vis=0;//这种方法TLE for(int i=0;i<26;i++) trie[0].son[i]=0; */ tot=ans=0;}void Insert(char *s){ int p=0; for(int i=0,len=strlen(s);i<len;i++){ int t=s[i]-'a'; if(!trie[p].son[t]) trie[p].son[t]=++tot; p=trie[p].son[t]; } trie[p].sum++;}void build_ac(){ queue<int> Q; for(int i=0;i<26;i++) if(trie[0].son[i]) Q.push(trie[0].son[i]); while(!Q.empty()){ int t=Q.front();Q.pop(); for(int i=0;i<26;i++){ int tmp=trie[t].son[i]; if(!tmp)continue; int f=trie[t].fail; while(f&&!trie[f].son[i]) f=trie[f].fail; trie[tmp].fail=trie[f].son[i]; Q.push(tmp); } }}int Query(char *str){ int p=0,ret=0; for(int i=0,len = strlen(str);i<len;i++){ int t=str[i]-'a'; while(p && !trie[p].son[t]) p=trie[p].fail; p=trie[p].son[t]; int tmp=p; while(tmp && !trie[tmp].vis){ ret+=trie[tmp].sum; trie[tmp].vis=true; tmp=trie[tmp].fail; } } return ret;}int T,n;char s[55],str[N];int main(){ cin>>T; while(T--){ init(); scanf("%d",&n); while(n --){ scanf(" %s",s); Insert(s); } build_ac(); scanf(" %s",str); printf("%d\n",Query(str)); } return 0;}
0 0
- hdu2222Keywords Search
- hdu2222Keywords Search
- hdu2222Keywords Search
- HDU2222Keywords Search
- Hdu2222Keywords Search
- hdu2222Keywords Search (字典树)
- hdu2222Keywords Search AC自动机
- 【AC自动机】HDU2222Keywords Search
- HDU2222Keywords Search [AC自动机]
- AC自动机-hdu2222Keywords Search
- hdu2222Keywords Search字典树入门……
- hdu2222Keywords Search AC自动机模板题
- HDU2222Keywords Search(AC自动机裸题)
- hdu2222Keywords Search(AC自动机模板题)
- hdu2222Keywords Search,caioj1464(AC自动机模板)
- Search
- search
- search
- hdu 3065 病毒侵袭持续中
- 2014年湖北省TI杯大学生电子设计竞赛论文格式
- HDU 4869 Turn the pokers
- poj3691DNA repair
- 信号完整性之差分对设计1(建立差分对)
- hdu2222Keywords Search
- 五大常用算法 之 动态规划法
- 算是个开头吧,在CSDN写点有的没的
- 信号完整性之差分对设计2(仿真前准备)
- 信号完整性之差分对设计3(仿真差分对)
- 信号完整性之差分对设计4(差分对约束)
- 信号完整性之差分对设计5(差分对布线)
- lower_bound()和upper_bound()
- 信号完整性之差分对设计6(后布线分析)