参考iwtwiioi的模板写出来的。上午gty讲的并没有听懂,只好自己慢慢对着模板理解。

在HDU上为什么相同的程序提交有时T有时A!!!

奉上sth神犇的模板(不是这道题):

  1. var
  2. ch:char;
  3. q,g,num:array [0..500001] of longint;
  4. st:string;
  5. son:array [0..500001,'a'..'z'] of longint;
  6. ts:array [0..1000001] of char;
  7. l,s,t,n,i,j,m,k,ans,head,tail:longint;
  8. begin
  9. readln(t);
  10. while t<>0 do
  11. begin
  12. for ch:='a' to 'z' do begin son[0,ch]:=1; son[1,ch]:=0; end;
  13. dec(t);
  14. s:=1;
  15. g[1]:=0;
  16. fillchar(num,sizeof(num),0);
  17. readln(n);
  18. for i:=1 to n do begin
  19. readln(st);
  20. m:=length(st);
  21. l:=1;
  22. for j:=1 to m do
  23. if son[l,st[j]]<>0 then l:=son[l,st[j]]
  24. else begin
  25. inc(s);
  26. g[s]:=0;
  27. son[l,st[j]]:=s;
  28. l:=s;
  29. for ch:='a' to 'z' do son[s,ch]:=0;
  30. end;
  31. inc(num[l]);
  32. end;
  33. s:=1;
  34. head:=0;
  35. tail:=1;
  36. q[1]:=1;
  37. n:=0;
  38. while head<tail do
  39. begin
  40. inc(head);
  41. k:=q[head];
  42. for ch:='a' to 'z' do
  43. if son[k,ch]=0 then son[k,ch]:=son[g[k],ch]
  44. else begin g[son[k,ch]]:=son[g[k],ch];
  45. inc(tail);
  46. q[tail]:=son[k,ch];
  47. end;
  48. end;

  

然后是我的ACcode:

  1. #include<queue>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #define for1(i,a,n) for(int i=(a);i<=(n);++i)
  6. #define for2(i,a,n) for(int i=(a);i<(n);++i)
  7. #define for3(i,a,n) for(int i=(a);i>=(n);--i)
  8. #define for4(i,a,n) for(int i=(a);i>(n);--i)
  9. #define CC(i,a) memset(i,a,sizeof(i));
  10. using namespace std;
  11. char a[10010],s[1000010];
  12. int ch[500010][30],fail[500010],w[500010],ans,cnt;
  13. inline void ins(char *b){
  14. int u=0,v,len=strlen(b);
  15. for2(i,0,len){
  16. v=b[i]-'a';
  17. if (!ch[u][v])ch[u][v]=cnt++;
  18. u=ch[u][v];
  19. }w[u]++;
  20. }
  21. inline void bfs(){
  22. queue<int>q;
  23. q.push(0);
  24. int u,p;
  25. while (!q.empty()){
  26. u=q.front();q.pop();
  27. for2(i,0,26) if (ch[u][i]){
  28. q.push(ch[u][i]);
  29. if (!u) continue;
  30. p=fail[u];
  31. while (p&&!ch[p][i])p=fail[p];
  32. fail[ch[u][i]]=ch[p][i];
  33. }
  34. }
  35. }
  36. inline void ac(char *b){
  37. int u=0,v,len=strlen(b),t;
  38. for2(i,0,len){
  39. v=b[i]-'a';
  40. while (u&&!ch[u][v])u=fail[u];
  41. u=ch[u][v];
  42. t=u;
  43. while (t){
  44. ans+=w[t];
  45. w[t]=0;
  46. t=fail[t];
  47. }
  48. }
  49. }
  50. inline void init(){CC(fail,0);CC(ch,0);CC(w,0);ans=0;cnt=1;}
  51. int main(){
  52. int T;scanf("%d",&T);
  53. while (T--){
  54. init(); int n;
  55. scanf("%d",&n);
  56. for1(i,1,n){
  57. scanf("%s",a);
  58. ins(a);
  59. }bfs();
  60. scanf("%s",s);
  61. ac(s);
  62. printf("%d\n",ans);
  63. }return 0;
  64. }

  

【HDU 2222】Keywords Search AC自动机模板题的更多相关文章

  1. HDU 2222 Keywords Search(AC自动机模板题)

    学习AC自动机请戳这里:大神blog........ 自动机的模板: #include <iostream> #include <algorithm> #include < ...

  2. HDU 2222 Keywords Search (AC自动机)(模板题)

    <题目链接> 题目大意: 给你一些单词,和一个字符串,问你这个字符串中含有多少个上面的单词. 解题分析: 这是多模匹配问题,如果用KMP的话,对每一个单词,都跑一遍KMP,那么当单词数量非 ...

  3. hdu 2222 Keywords Search ac自动机模板

    题目链接 先整理一发ac自动机模板.. #include <iostream> #include <vector> #include <cstdio> #inclu ...

  4. hdu 2222 Keywords Search ac自动机入门

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:有N(N <= 10000)个长度不超过50的模式串和一个长度不超过1e6的文本串. ...

  5. hdu 2222 Keywords Search——AC自动机

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=2222 第一道AC自动机! T了无数边后终于知道原来它是把若干询问串建一个自动机,把模式串放在上面跑:而且只 ...

  6. HDU 2222 Keywords Search (AC自动机)

    题意:就是求目标串中出现了几个模式串. 思路:用int型的end数组记录出现,AC自动机即可. #include<iostream> #include<cstdio> #inc ...

  7. Match:Keywords Search(AC自动机模板)(HDU 2222)

    多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...

  8. POJ2222 Keywords Search AC自动机模板

    http://acm.hdu.edu.cn/showproblem.php?pid=2222 题意:给出一些单词,求多少个单词在字符串中出现过(单词表单词可能有相同的,这些相同的单词视为不同的分别计数 ...

  9. Keywords Search(AC自动机模板)

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

随机推荐

  1. NOIP2010提高组 关押罪犯 -SilverN

    (洛谷P1525) 题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用“怨气值”( ...

  2. 《MapReduce: Simplified Data Processing on Large Cluster 》翻译

    Abstract MapReduce是一种编程模型和一种用来处理和产生大数据集的相关实现.用户定义map函数来处理key/value键值对来产生一系列的中间的key/value键值对.还要定义一个re ...

  3. UVALive 6263 The Dragon and the knights --统计,直线分平面

    题意:给n条直线,将一个平面分成很多个部分,再给m个骑士的坐标,在一个部分内只要有一个骑士即可保护该部分,问给出的m个骑士是不是保护了所有部分. 解法:计算每个骑士与每条直线的位置关系(上面还是下面) ...

  4. 线性代数与MATALB1

    1图论的一个基本应用 下图描述了4个城市之间的航空航线图, 为了描述着4个城市之间航线的邻接关系,定义邻接矩阵 第i行描述从城市i出发,可以达到各个城市的情况, 可以证明,矩阵A^N表示一个人连续坐N ...

  5. AC日记——搞笑世界杯 codevs 1060(dp)

    题目描述 Description 随着世界杯小组赛的结束,法国,阿根廷等世界强队都纷纷被淘汰,让人心痛不已. 于是有 人组织了一场搞笑世界杯,将这些被淘汰的强队重新组织起来和世界杯一同比赛.你和你的朋 ...

  6. Android 屏幕适配(二)增强版百分比布局库(percent-support-lib)

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/46767825: 本文出自:[张鸿洋的博客] 一 概述 上周一我们发布了Andr ...

  7. css3新属性的总结

    今天继续总结css3的一些css3新样式,先列一个简单的提纲,重要的还是圆角.阴影.渐变.文字缩略,最最重要的是过度transition,变换transform和animation圆角阴影渐变 圆形渐 ...

  8. 工厂模式(Factory Patter)

    1.工厂模式简介 工厂模式属于创建型模式,是专门用来创建对象的模式,抽象了实例化的过程.工厂模式分为 : 工厂方法模式.抽象工厂模式. 在学习工厂方法模式.抽象工厂之前,首先先要了解一下简单工厂模式, ...

  9. Java的IO操作---File类

    目标 1)掌握File类作用 2)可以使用file类中方法对文件进行读写操作. File类 唯一与文件有关的类.使用file类可进行创建或删除操作,要想使用File类,首先观察File类的构造方法. ...

  10. ArcGis实现添加MultiLayerMarkerSymbol(多个符号叠加生成新的符号)

    , , );             pMarkerSymbol.Angle = ;             pMarkerSymbol.XOffset = ;;;;, , );            ...