正反两遍扩展KMP,维护公共长度为L时。出如今最左边和最右边的位置。

。。

然后枚举推断。。。

E. Martian Strings
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

During the study of the Martians Petya clearly understood that the Martians are absolutely lazy. They like to sleep and don't like to wake up.

Imagine a Martian who has exactly n eyes located in a row and numbered from the left to the right from 1 to n.
When a Martian sleeps, he puts a patch on each eye (so that the Martian morning doesn't wake him up). The inner side of each patch has an uppercase Latin letter. So, when a Martian wakes up and opens all his eyes he sees a string s consisting
of uppercase Latin letters. The string's length isn.

"Ding dong!" — the alarm goes off. A Martian has already woken up but he hasn't opened any of his eyes. He feels that today is going to be a hard day, so he wants to open his eyes and see something good. The Martian considers only m Martian
words beautiful. Besides, it is hard for him to open all eyes at once so early in the morning. So he opens two non-overlapping segments of consecutive eyes. More formally, the Martian chooses four numbers abcd,
(1 ≤ a ≤ b < c ≤ d ≤ n) and opens all eyes with numbers i such
that a ≤ i ≤ b or c ≤ i ≤ d. After
the Martian opens the eyes he needs, he reads all the visible characters from the left to the right and thus, he sees some word.

Let's consider all different words the Martian can see in the morning. Your task is to find out how many beautiful words are among them.

Input

The first line contains a non-empty string s consisting of uppercase Latin letters. The strings' length is n (2 ≤ n ≤ 105).
The second line contains an integer m (1 ≤ m ≤ 100)
— the number of beautiful words. Next m lines contain the beautiful words pi,
consisting of uppercase Latin letters. Their length is from 1 to 1000.
All beautiful strings are pairwise different.

Output

Print the single integer — the number of different beautiful strings the Martian can see this morning.

Sample test(s)
input
  1. ABCBABA
  2. 2
  3. BAAB
  4. ABBA
output
  1. 1
Note

Let's consider the sample test. There the Martian can get only the second beautiful string if he opens segments of eyes a = 1, b = 2 and c = 4, d = 5 or
of he opens segments of eyes a = 1, b = 2 and c = 6, d = 7.

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. const int INF=0x3f3f3f3f;
  9.  
  10. const int maxn=101000;
  11.  
  12. char T[maxn],P[maxn/100];
  13. int next[maxn/10],ex[maxn];
  14.  
  15. char rT[maxn],rP[maxn/100];
  16. int rnext[maxn/100],rex[maxn];
  17. int LEFT[maxn/100],RIGHT[maxn/100];
  18.  
  19. int n,q,m;
  20.  
  21. void pre_exkmp(int next[],char P[],int m)
  22. {
  23. next[0]=m;
  24. int j=0,k=1;
  25. while(j+1<m&&P[j]==P[j+1]) j++;
  26. next[1]=j;
  27. for(int i=2;i<m;i++)
  28. {
  29. int p=next[k]+k-1;
  30. int L=next[i-k];
  31. if(i+L<p+1) next[i]=L;
  32. else
  33. {
  34. j=max(0,p-i+1);
  35. while(i+j<m&&P[i+j]==P[j]) j++;
  36. next[i]=j; k=i;
  37. }
  38. }
  39. }
  40.  
  41. void get_limit(int kind,int pos,int ex)
  42. {
  43. if(kind==0)
  44. {
  45. if(LEFT[ex]==INF)
  46. {
  47. LEFT[ex]=pos;
  48. }
  49. }
  50. else if(kind==1)
  51. {
  52. if(RIGHT[ex]==-1)
  53. {
  54. RIGHT[ex]=n-1-pos;
  55. }
  56. }
  57. }
  58.  
  59. void gao(int kind,int m)
  60. {
  61. if(kind==1)
  62. {
  63. int last=-1;
  64. for(int i=m;i>=1;i--)
  65. {
  66. if(RIGHT[i]<last)
  67. {
  68. RIGHT[i]=last;
  69. }
  70. else
  71. {
  72. last=RIGHT[i];
  73. }
  74. }
  75. }
  76. else
  77. {
  78. int last=INF;
  79. for(int i=m;i>=1;i--)
  80. {
  81. if(LEFT[i]>last)
  82. {
  83. LEFT[i]=last;
  84. }
  85. else
  86. {
  87. last=LEFT[i];
  88. }
  89. }
  90. }
  91. }
  92.  
  93. ///kind 0...LEFT 1...RIGHT
  94. void exkmp(int kind,int& mx,int ex[],int next[],char P[],char T[],int n,int m)
  95. {
  96. pre_exkmp(next,P,m);
  97. int j=0,k=0;
  98. while(j<n&&j<m&&P[j]==T[j]) j++;
  99. ex[0]=j;
  100.  
  101. mx=max(mx,ex[0]);
  102. get_limit(kind,0,ex[0]);
  103.  
  104. for(int i=1;i<n;i++)
  105. {
  106. int p=ex[k]+k-1;
  107. int L=next[i-k];
  108. if(i+L<p+1) ex[i]=L;
  109. else
  110. {
  111. j=max(0,p-i+1);
  112. while(i+j<n&&j<m&&T[i+j]==P[j]) j++;
  113. ex[i]=j; k=i;
  114. }
  115. mx=max(mx,ex[i]);
  116. get_limit(kind,i,ex[i]);
  117. }
  118. }
  119.  
  120. int main()
  121. {
  122. scanf("%s",T);
  123. n=strlen(T);
  124. for(int i=0;i<n;i++)
  125. rT[i]=T[n-1-i];
  126. int ans=0;
  127. scanf("%d",&q);
  128. while(q--)
  129. {
  130. scanf("%s",P);
  131. int m=strlen(P);
  132. for(int i=0;i<m;i++)
  133. rP[i]=P[m-1-i];
  134.  
  135. memset(LEFT,63,sizeof(LEFT));
  136. memset(RIGHT,-1,sizeof(RIGHT));
  137.  
  138. int mx1=0,mx2=0;
  139. exkmp(0,mx1,ex,next,P,T,n,m);
  140. gao(0,m);
  141. exkmp(1,mx2,rex,rnext,rP,rT,n,m);
  142. gao(1,m);
  143.  
  144. if(mx1+mx2<m) continue;
  145.  
  146. bool flag=false;
  147.  
  148. for(int len=1;len<m&&flag==false;len++)
  149. {
  150.  
  151. if(LEFT[len]==INF||RIGHT[m-len]==-1) continue;
  152. if(LEFT[len]+len-1<RIGHT[m-len]-(m-len)+1)
  153. {
  154. ans++;
  155. flag=true;
  156. }
  157. }
  158. }
  159. printf("%d\n",ans);
  160. return 0;
  161. }

Codeforces 149 E. Martian Strings的更多相关文章

  1. CodeForces 149E Martian Strings exkmp

    Martian Strings 题解: 对于询问串, 我们可以从前往后先跑一遍exkmp. 然后在倒过来,从后往前跑一遍exkmp. 我们就可以记录下 对于每个正向匹配来说,最左边的点在哪里. 对于每 ...

  2. xtu summer individual-4 D - Martian Strings

    Martian Strings Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. ...

  3. Codeforces 385B Bear and Strings

    题目链接:Codeforces 385B Bear and Strings 记录下每一个bear的起始位置和终止位置,然后扫一遍记录下来的结构体数组,过程中用一个变量记录上一个扫过的位置,用来去重. ...

  4. Codeforces 482C Game with Strings(dp+概率)

    题目链接:Codeforces 482C Game with Strings 题目大意:给定N个字符串,如今从中选定一个字符串为答案串,你不知道答案串是哪个.可是能够通过询问来确定, 每次询问一个位置 ...

  5. codeforces 149E . Martian Strings kmp

    题目链接 给一个字符串s, n个字符串str. 令tmp为s中不重叠的两个连续子串合起来的结果, 顺序不能改变.问tmp能形成n个字符串中的几个. 初始将一个数组dp赋值为-1. 对str做kmp, ...

  6. 【24.34%】【codeforces 560D】Equivalent Strings

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. CodeForces 682D Alyona and Strings (四维DP)

    Alyona and Strings 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/D Description After re ...

  8. codeforces 518A. Vitaly and Strings

    A. Vitaly and Strings time limit per test 1 second memory limit per test 256 megabytes input standar ...

  9. Codeforces 985 F - Isomorphic Strings

    F - Isomorphic Strings 思路:字符串hash 对于每一个字母单独hash 对于一段区间,求出每个字母的hash值,然后排序,如果能匹配上,就说明在这段区间存在字母间的一一映射 代 ...

随机推荐

  1. SQL Server Backup & Restore

    USE [master]; GO CREATE DATABASE test; GO CREATE DATABASE test2; GO BACKUP DATABASE test TO DISK = ' ...

  2. Mysql 删除重复记录,只保留最小的一条

    delete from `jb_postcontent` where id not in(select min(id) from (select * from `jb_postcontent`) as ...

  3. 【译】Asp.Net Identity Cookies 格式化-中英对照版

    原文出处 Trailmax Tech Max Vasilyev: ASP.Net MVC development in Aberdeen, Scotland I've been reached out ...

  4. 《Linux命令行与shell脚本编程大全》 第六章环境变量

    很多程序和脚本都通过环境变量来获取系统信息.存储临时数据和配置信息. 6.1 什么是环境变量: bash shell用一个叫环境变量(environment variable)的特性来存储有关shel ...

  5. Linux修改date

    与cn校时 ntpdate cn.pool.ntp.org 设置时区 TZ='Asia/Shanghai'; export TZ 刷新生效 source ~/.bashrc vi /etc/profi ...

  6. JS中有关数组Array的常用方法函数

    Array对象的方法主要有如下几种(我所知道的): concat()连接两个或多个数组,并返回结果,但是值得注意的是该方法并不改变数组本身,而仅仅返回一个数组连接的副本. push()在数组后面添加一 ...

  7. shell的EOF用法

    将命令输出的结果给一个循环处理,常用的方式如下: [root@etch171 guosong]# ls |while read line;do echo $line;done processlist ...

  8. ASP.NET Core 认证与授权[5]:初识授权

    经过前面几章的姗姗学步,我们了解了在 ASP.NET Core 中是如何认证的,终于来到了授权阶段.在认证阶段我们通过用户令牌获取到用户的Claims,而授权便是对这些的Claims的验证,如:是否拥 ...

  9. 【Java入门提高篇】Day1 抽象类

    基础部分内容差不多讲解完了,今天开始进入Java提高篇部分,这部分内容会比之前的内容复杂很多,希望大家做好心理准备,看不懂的部分可以多看两遍,仍不理解的部分那一定是我讲的不够生动,记得留言提醒我. 好 ...

  10. maven学习之二

    三 profile介绍 可以有多个地方定义profile.定义的地方不同,它的作用范围也不同. (1)    针对于特定项目的profile配置我们可以定义在该项目的pom.xml中. (2)     ...