Oulipo
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 40168   Accepted: 16135

Description

The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote from the book:

Tout avait Pair normal, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, puis surgissait l’inhumain, l’affolant. Il aurait voulu savoir où s’articulait l’association qui l’unissait au roman : stir son tapis, assaillant à tout instant son imagination, l’intuition d’un tabou, la vision d’un mal obscur, d’un quoi vacant, d’un non-dit : la vision, l’avision d’un oubli commandant tout, où s’abolissait la raison : tout avait l’air normal mais…

Perec would probably have scored high (or rather, low) in the following contest. People are asked to write a perhaps even meaningful text on some subject with as few occurrences of a given “word” as possible. Our task is to provide the jury with a program that counts these occurrences, in order to obtain a ranking of the competitors. These competitors often write very long texts with nonsense meaning; a sequence of 500,000 consecutive 'T's is not unusual. And they never use spaces.

So we want to quickly find out how often a word, i.e., a given string, occurs in a text. More formally: given the alphabet {'A', 'B', 'C', …, 'Z'} and two finite strings over that alphabet, a word W and a text T, count the number of occurrences of W in T. All the consecutive characters of W must exactly match consecutive characters of T. Occurrences may overlap.

Input

The first line of the input file contains a single number: the number of test cases to follow. Each test case has the following format:

  • One line with the word W, a string over {'A', 'B', 'C', …, 'Z'}, with 1 ≤ |W| ≤ 10,000 (here |W| denotes the length of the string W).
  • One line with the text T, a string over {'A', 'B', 'C', …, 'Z'}, with |W| ≤ |T| ≤ 1,000,000.

Output

For every test case in the input file, the output should contain a single number, on a single line: the number of occurrences of the word W in the text T.

Sample Input

3
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN

Sample Output

1
3
0

Source

 
莫名其妙TLE
 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char a[];
char b[];
int p[];
int la,lb;
int j;
int ans;
inline void makep()
{ j=;
for(int i=;i<la;i++)
{
while(j>&&a[i]!=a[j])
j=p[j];
if(a[i]==a[j])
j++;
p[i]=j;
}
return ;
}
inline void KMP()
{
j=;
ans=;
for(int i=;i<lb;i++)
{
while(b[i]!=a[j]&&j>)
j=p[j-];
if(b[i]==a[j])
j++;
if(j==la)
{
ans++;
j=p[j-];
} }
printf("%d\n",ans);
return ;
} int main()
{
int n;
scanf("%d",&n); for(int i=;i<=n;i++)
{
memset(p,,sizeof(p));
scanf("%s%s",a,b);
la=strlen(a);lb=strlen(b);
makep();
KMP();
} return ;
}

TLE

AC

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std; int n,la,lb;
int next[];
char a[],b[]; void getnext()
{
int j=-;
next[]=-;
for(int i=;i<lb;i++)
{
if(j!=- && b[j+]!=b[i]) j=next[j];
if(b[j+]==b[i]) j++;
next[i]=j;
}
} int kmp()
{
int ans=;
int j=-;
for(int i=;i<la;i++)
{
if(j!=- && a[i]!=b[j+]) j=next[j];
if(b[j+]==a[i]) j++;
if(j==lb-)
{
ans++;
j=next[j];
}
}
return ans;
} int main()
{
scanf("%d",&n);
for(int u=;u<=n;u++)
{
scanf("%s %s",b,a);
la=strlen(a);
lb=strlen(b);
getnext();
printf("%d\n",kmp());
}
}

AC

最新文章

  1. Test your application
  2. Swift 中的指针使用
  3. c中三大区的解析
  4. HTML5的新特性及技巧分享总结
  5. Windows Azure Camp---漫步云端,创意无限
  6. Android模拟器PANIC: Could not open:问题解决方法
  7. GitHub前50名的Objective-C动画相关库相关推荐,请自行研究
  8. 30个值得关注的Vue开源项目
  9. redis 在 php 中的应用(key篇)
  10. BottomNavigationBarItem fixed
  11. 【转】RCP中org.eclipse.core.runtime.CoreException
  12. Django学习手册 - 自定义分页函数
  13. code vs 3492 细胞个数
  14. S3C6410裸奔之旅——RVDS2.2编译、仿真、调试过程 LED流水灯---转的
  15. SQLSERVER中order by ,group by ,having where 的先后顺序
  16. Gitlab CI-3.遇到的问题
  17. mysql show processlist 命令检查mysql lock
  18. 从golang-gin-realworld-example-app项目学写httpapi (二)
  19. Android调用系统拍照裁剪和选图功能
  20. 3天学习完AngularJS基础内容小结

热门文章

  1. Flutter实战视频-移动电商-66.会员中心_编写ListTile通用方法
  2. 1-1 课程简介 &amp; 2-1 IDEA与Eclipse的不同 &amp; 2-3 Intellij IDEA安装
  3. Maven面试必备
  4. std::map的删除
  5. uoj #210. 【UER #6】寻找罪犯【2-SAT】
  6. bzoj 3653: 谈笑风生【dfs序+主席树】
  7. P4363 [九省联考2018]一双木棋chess(对抗搜索+记忆化搜索)
  8. python 函数 之 用户注册register()
  9. 黑马函数式接口学习 Stream流 函数式接口 Lambda表达式 方法引用
  10. java数据结构----树