意甲冠军:

到原始字符串。给n字符串,每个字符串都有一个属性,属性0代表重叠,1代表不能重叠

请各多少次出现的字符串

思维:

为了便于建立两台机器自己主动(0一个。1一个)

然后,它可以重叠非常好做,谁做

不可重叠的话须要记录两个东西

len[i]代表每一个串的长度,used[i]代表每一个串在之前出现的位置,初始化-1

然后遍历到的时候对于当前位置 j。 必须j>=used[i]+len[i] 才干算出现。而且更新

须要注意的是:

会出现相同属性而且相同的串。

我处理的方式就是排序。按id排序大的在前

然后算一遍大的,用大的赋值给id 小的且串同样的。

代码:

  1. #include"cstdlib"
  2. #include"cstdio"
  3. #include"cstring"
  4. #include"cmath"
  5. #include"queue"
  6. #include"algorithm"
  7. #include"iostream"
  8. using namespace std;
  9. char fuck[123456];
  10. int ans[123456],used[123456],len[123456];
  11. struct word
  12. {
  13. int x,id;
  14. char y[7];
  15. } dc[123456];
  16. struct trie
  17. {
  18. int mark;
  19. trie *next[27];
  20. trie *fail;
  21. trie()
  22. {
  23. mark=0;
  24. memset(next,0,sizeof(next));
  25. fail=NULL;
  26. }
  27. };
  28. trie *root0,*root1;
  29. void init(int key,char *v,int id)
  30. {
  31. trie *p;
  32. if(key) p=root1;
  33. else p=root0;
  34. for(int i=0; v[i]; i++)
  35. {
  36. int tep=v[i]-'a';
  37. if(p->next[tep]==NULL) p->next[tep]=new trie();
  38. p=p->next[tep];
  39. }
  40. p->mark=id;
  41. }
  42. void del(trie *p)
  43. {
  44. for(int j=0; j<26; j++) if(p->next[j]!=NULL) del(p->next[j]);
  45. free(p);
  46. }
  47. void getac()
  48. {
  49. queue<trie*>q;
  50. q.push(root0);
  51. while(!q.empty())
  52. {
  53. trie *p,*tep;
  54. p=q.front();
  55. q.pop();
  56. for(int i=0; i<26; i++)
  57. {
  58. if(p->next[i]!=NULL)
  59. {
  60. if(p==root0) p->next[i]->fail=root0;
  61. else
  62. {
  63. tep=p->fail;
  64. while(tep!=NULL)
  65. {
  66. if(tep->next[i]!=NULL)
  67. {
  68. p->next[i]->fail=tep->next[i];
  69. break;
  70. }
  71. tep=tep->fail;
  72. }
  73. if(tep==NULL) p->next[i]->fail=root0;
  74. }
  75. q.push(p->next[i]);
  76. }
  77. }
  78. }
  79. q.push(root1);
  80. while(!q.empty())
  81. {
  82. trie *p,*tep;
  83. p=q.front();
  84. q.pop();
  85. for(int i=0; i<26; i++)
  86. {
  87. if(p->next[i]!=NULL)
  88. {
  89. if(p==root1) p->next[i]->fail=root1;
  90. else
  91. {
  92. tep=p->fail;
  93. while(tep!=NULL)
  94. {
  95. if(tep->next[i]!=NULL)
  96. {
  97. p->next[i]->fail=tep->next[i];
  98. break;
  99. }
  100. tep=tep->fail;
  101. }
  102. if(tep==NULL) p->next[i]->fail=root1;
  103. }
  104. q.push(p->next[i]);
  105. }
  106. }
  107. }
  108. }
  109. void finde(char *v)
  110. {
  111. trie *p0=root0,*p1=root1;
  112. for(int i=0; v[i]; i++)
  113. {
  114. int tep=v[i]-'a';
  115. while(p0->next[tep]==NULL && p0!=root0)
  116. p0=p0->fail;
  117. p0=p0->next[tep];
  118. if(p0==NULL) p0=root0;
  119. trie *q0=p0;
  120. while(q0!=root0)
  121. {
  122. if(q0->mark!=0) ans[q0->mark]++;
  123. q0=q0->fail;
  124. }
  125. while(p1->next[tep]==NULL && p1!=root1)
  126. p1=p1->fail;
  127. p1=p1->next[tep];
  128. if(p1==NULL) p1=root1;
  129. trie *q1=p1;
  130. while(q1!=root1)
  131. {
  132. if(q1->mark!=0)
  133. {
  134. if(i>=used[q1->mark]+len[q1->mark]) //不可重叠的推断
  135. {
  136. ans[q1->mark]++;
  137. used[q1->mark]=i;
  138. }
  139. }
  140. q1=q1->fail;
  141. }
  142. }
  143. }
  144. int cmp(word a,word b) //排序的cmp
  145. {
  146. if(a.x==b.x)
  147. {
  148. if(strcmp(a.y,b.y)==0)
  149. {
  150. if(a.id>b.id) return 1;
  151. else return 0;
  152. }
  153. else
  154. {
  155. if(strcmp(a.y,b.y)>0) return 1;
  156. else return 0;
  157. }
  158. }
  159. else
  160. {
  161. if(a.x>b.x) return 1;
  162. else return 0;
  163. }
  164. }
  165. int main()
  166. {
  167. int cas=1;
  168. while(scanf("%s",fuck)!=-1)
  169. {
  170. int n;
  171. scanf("%d",&n);
  172. root0=new trie();
  173. root1=new trie();
  174. for(int i=1; i<=n; i++)
  175. {
  176. int x;
  177. char y[12];
  178. scanf("%d%s",&x,y);
  179. len[i]=strlen(y);
  180. init(x,y,i);
  181. dc[i].x=x;
  182. strcpy(dc[i].y,y);
  183. dc[i].id=i;
  184. }
  185. memset(ans,0,sizeof(ans));
  186. memset(used,-1,sizeof(used));
  187. getac();
  188. finde(fuck);
  189. sort(dc+1,dc+1+n,cmp);
  190. int i;
  191. for(i=1; dc[i+1].x==1; i++) //赋值给那些反复的
  192. {
  193. if(strcmp(dc[i].y,dc[i+1].y)==0)
  194. ans[dc[i+1].id]=ans[dc[i].id];
  195. }
  196. for(i=i+1; i<n; i++)
  197. {
  198. if(strcmp(dc[i].y,dc[i+1].y)==0)
  199. ans[dc[i+1].id]=ans[dc[i].id];
  200. }
  201. printf("Case %d\n",cas++);
  202. for(int i=1; i<=n; i++) printf("%d\n",ans[i]);
  203. del(root0);
  204. del(root1);
  205. puts("");
  206. }
  207. return 0;
  208. }

版权声明:本文博客原创文章,博客,未经同意,不得转载。

[AC自己主动机] zoj Searching the String的更多相关文章

  1. ZOJ 3228 Searching the String (AC自己主动机)

    题目链接:Searching the String 解析:给一个长串.给n个不同种类的短串.问分别在能重叠下或者不能重叠下短串在长串中出现的次数. 能重叠的已经是最简单的AC自己主动机模板题了. 不能 ...

  2. ZOJ - 3228 Searching the String (AC自己主动机)

    Description Little jay really hates to deal with string. But moondy likes it very much, and she's so ...

  3. zoj 3430 Detect the Virus(AC自己主动机)

    Detect the Virus Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Nobita found that his co ...

  4. ZOJ 3494 BCD Code (AC自己主动机 + 数位DP)

    题目链接:BCD Code 解析:n个病毒串.问给定区间上有多少个转换成BCD码后不包括病毒串的数. 很奇妙的题目. . 经典的 AC自己主动机 + 数位DP 的题目. 首先使用AC自己主动机,得到b ...

  5. AC自己主动机

    AC自己主动机 AC自己主动机是KMP和Trie的结合,主要处理多模板串匹配问题.以下推荐一个博客,有助于学习AC自己主动机. NOTONLYSUCCESS  这里另一个Kuangbin开的比赛,大家 ...

  6. POJ 2778 DNA Sequence (AC自己主动机 + dp)

    DNA Sequence 题意:DNA的序列由ACTG四个字母组成,如今给定m个不可行的序列.问随机构成的长度为n的序列中.有多少种序列是可行的(仅仅要包括一个不可行序列便不可行).个数非常大.对10 ...

  7. 【UVA】1449-Dominating Patterns(AC自己主动机)

    AC自己主动机的模板题.须要注意的是,对于每一个字符串,须要利用map将它映射到一个结点上,这样才干按顺序输出结果. 14360841 1449 option=com_onlinejudge& ...

  8. POJ 3691 &amp; HDU 2457 DNA repair (AC自己主动机,DP)

    http://poj.org/problem?id=3691 http://acm.hdu.edu.cn/showproblem.php?pid=2457 DNA repair Time Limit: ...

  9. HDU 2896 病毒侵袭 AC自己主动机题解

    本题是在text里面查找key word的增强版.由于这里有多个text. 那么就不能够简单把Trie的叶子标志记录改动成-1进行加速了,能够使用其它技术.我直接使用个vis数组记录已经訪问过的节点, ...

随机推荐

  1. 【u124】环状最大两段子段和

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 给出一段环状序列,即认为A[1]和A[N]是相邻的,选出其中连续不重叠且非空的两段使得这两段和最大. ...

  2. IAdjustCountOption--动态设置recycleView的itemCount(不须要改动数据源)

    概述 RecycleViewUtil是新增的一个主要针对RecycleView的一个工具类.该工具类中提供了部分RecycleView可能会使用到的方法,当中也包含了一些用来增强HeaderRecyc ...

  3. 菜单之一:Menu基础内容 分类: H1_ANDROID 2013-11-03 00:23 906人阅读 评论(0) 收藏

    参考<疯狂android讲义>2.10节P168 1.重要接口 Android菜单相关的重要接口共有以下四个: 其中Menu为普通菜单,SubMenu包含子项,ContextMenu当长时 ...

  4. js课程 1-5 js如何测试变量的数据类型

    js课程 1-5 js如何测试变量的数据类型 一.总结 一句话总结:用typeof()方法. 1.js如何判断变量的数据类型? 用typeof()方法. 13 v=10; 14 15 if(typeo ...

  5. jquery 多选框 checkbox 获取选中的框

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. &lt;LeetCode OJ&gt; 62. / 63. Unique Paths(I / II)

    62. Unique Paths My Submissions Question Total Accepted: 75227 Total Submissions: 214539 Difficulty: ...

  7. [Angular] Scrolling the Message List To the Bottom Automatically Using OnChanges

    Let's say the message list can Input (messages) from parent component, and what we want to do is whe ...

  8. 小强的HTML5移动开发之路(43)——JqueryMobile页眉、工具栏和标签栏导航

    一.页眉 1.添加页眉和页脚 <div data-role="header"> <h1>第 1 页</h1> </div> < ...

  9. [Postgre] Insert Data into Postgre Tables

    // Insert one row INSERT INTO movies (title, release_date, count_stars, director_id) VALUES ( 'Kill ...

  10. 第三十一天 慵懒的投射在JDBC上的暖阳 —Hibernate的使用(四)

    6月19日,小雨."黄梅时节家家雨.青草池塘处处蛙.有约不来过夜半,闲敲棋子落灯花." 面向对象无限包容的个性,给对SQL和数据库一窍不通的澳大利亚人Gavin King创造了极大 ...