Problem 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

题意:t 组数据,每组给出两个字符串,第一个字符串是模式串,第二个字符串是文本串,求匹配串再目标串中出现次数,若没有则输出 0

思路:求文本串中模式串出现次数,允许使用重复的字符,此外注意模式串和文本串的位置即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define PI acos(-1.0)
#define E 1e-9
#define INF 0x3f3f3f3f
#define N 1000001
#define LL long long
const int MOD=20091226;
const int dx[]= {-1,1,0,0};
const int dy[]= {0,0,-1,1};
using namespace std;int Next[N];
char str1[N],str2[N];
void getNext(char p[]){Next[0]=-1;int len=strlen(p);int j=0;int k=-1;while(j<len){if(k==-1||p[j]==p[k]) {k++;j++;Next[j]=k;}else{k=Next[k];}}
}
int KMP(char t[],char p[]) {int tLen=strlen(t);int pLen=strlen(p);getNext(p);int res=0;int j=0;for(int i=0;i<tLen;i++){while(j&&p[j]!=t[i]){j=Next[j];}if(p[j]==t[i]){j++;}if(j==pLen){res++;}}return res;
}int main(){int t;scanf("%d",&t);while(t--){scanf("%s",str1);scanf("%s",str2);printf("%d\n",KMP(str2,str1));}return 0;
}

Oulipo(POJ-3461)相关推荐

  1. Bailian2734 十进制到八进制【入门】(POJ NOI0113-45)

    问题链接:POJ NOI0113-45十进制到八进制 2734:十进制到八进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个十进制正整数转化成八进制. 输入 一行,仅含一个十进 ...

  2. Bailian2676 整数的个数【入门】(POJ NOI0105-11)

    问题链接:POJ NOI0105-11 整数的个数 2676:整数的个数 总时间限制: 1000ms 内存限制: 65536kB 描述 给定k(1 < k < 100)个正整数,其中每个数 ...

  3. Bailian4029 数字反转【进制】(POJ NOI0105-29)

    问题链接:POJ NOI0105-29 数字反转 4029:数字反转 总时间限制: 1000ms 内存限制: 65535kB 描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数 ...

  4. Bailian2735 八进制到十进制【入门】(POJ NOI0113-46)

    问题链接:POJ NOI0113-46 八进制到十进制 2735:八进制到十进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个八进制正整数转化成十进制. 输入 一行,仅含一个八 ...

  5. Oulipo(欧力波)(经典kmp模板题) HDU-1686

    题目:Oulipo(欧力波) 中文大意 The French author Georges Perec (1936�C1982) once wrote a book, La disparition, ...

  6. Silver Cow Party (POJ - 3268 )

    Silver Cow Party (POJ - 3268 ) 这道题是我做的最短路专题里的一道题,但我还没做这个,结果比赛就出了,真是.......... 题目: One cow from each ...

  7. 吴昊品游戏核心算法 Round 7 —— 熄灯游戏AI(有人性的Brute Force)(POJ 2811)

    暴力分为两种,一种属于毫无人性的暴力,一种属于有人性 的暴力.前面一种就不说了,对于后面一种情况,我们可以只对其中的部分问题进行枚举,而通过这些子问题而推导到整个的问题中.我称之为有人性的Brute ...

  8. 【二分】Best Cow Fences(poj 2018)

    Best Cow Fences poj 2018 题目大意: 给出一个正整数数列,要你求平均数最大,长度不小于M的字串,结果乘1000取整 输入样例 10 6 6 4 2 10 3 8 5 9 4 1 ...

  9. 昂贵的聘礼(poj 1062)

    Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低 ...

  10. 主席树学习小结(POJ 2104)

    在高中的时候就听到过主席树了,感觉非常高端,在寒假的时候 winter homework中有一题是查找区间第K大的树,当时就开始百度这种网上的博客,发现主席树看不懂,因为那个root[i],还有tx[ ...

最新文章

  1. Hypertable hbase hdfs kfs java与c++的较量
  2. 自反访问控制列表(ACL)
  3. java 防并发_并发:如何防止两个不同类中的两个方法同时运行?
  4. 61二叉搜索树的第k个结点
  5. redis一般缓存什么样数据_SpringBoot+Redis轻松实现数据缓存
  6. akcms在模板文件中书写{php},在模版中灵活处理变量的4种方法
  7. 【C#】图片处理(底片,黑白,锐化,柔化,浮雕,雾化)
  8. 免费正版 Win 10/8/7操作系统虚拟机镜像下载
  9. java有哪些字体_java字体有哪些
  10. jQuery中的日期时间控件
  11. U盘病毒专杀工具(usbcleaner)(绿色版)
  12. 阿里云注册域名,购买云服务器,备案,域名解析教程
  13. Python实现日程表
  14. UINO优锘:干货分享 | 虚拟现实建模基础
  15. 处理一份内心煎熬的工作有两种方法——只有一种是正确的
  16. 在字节跳动“混”了2年软件测试岗,被辞之后我承认我后悔了...
  17. java(jfinal) 接入ios苹果内购(连续包月订阅),服务端将二次验证。
  18. 能装linux的嵌入式,试试一张软盘可装下Linux(嵌入式Linux)
  19. jmeter测试服务器性能测试报告,Jmeter的性能测试
  20. java 之 xml 解析工具

热门文章

  1. 戏耍Transaction,多个连接的Transaction处理(非COM+)
  2. Java笔记(四)各类容器,set,map,队列实现
  3. 进厂打工的大学生:每天站12个小时,4年存50万
  4. Python成为TIOBE 2020年度编程语言!是获此奖项次数最多的语言
  5. 什么是引力波?它是怎么被发现的?
  6. 「最有用」的特殊大数据:一文看懂文本信息系统的概念框架及功能
  7. 本地计算机上的mysql服务怎么注册,本地计算机上的mysql服务启动后中止
  8. 新年快乐,送一台新款华为笔记本!
  9. Spring5 新增的两大功能,吹一波这个框架!
  10. 别再瞎搞了,处理Java异常的10个最佳实践