Oulipo POJ - 3461(kmp,求重叠匹配个数)
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
题意:
给出T组数据,每组数据有两个字符串,让我们查找第一个字符串在第二个字符串中出现的次数,可重叠;
思路:kmp;
注意:使用C++中cin、cout作为输入输出语句将导致超时,也是怪坑的
具体代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxx 2000100
char a[maxx],b[];
int n,m;
int p[maxx];
void pre()
{
int i=,j=-;
p[]=-;
while(i<m)
{
if(j==-||b[i]==b[j])
{
j++,i++;
p[i]=j;
}
else
j=p[j];
}
}
void kmp()
{
int i=,j=,ans=;
while(i<n)
{
if(j==-||a[i]==b[j])
{
i++,j++;
if(j==m)
{
ans++;
j=p[j];
}
}
else
j=p[j];
}
printf("%d\n",ans);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s%s",b,a);
n=strlen(a);//
m=strlen(b);
pre();
kmp();
}
return ;
}
下面来一道求不重叠部分的题目
Oulipo POJ - 3461(kmp,求重叠匹配个数)的更多相关文章
- 剪花布条 HDU - 2087(kmp,求不重叠匹配个数)
Problem Description 一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input 输入 ...
- HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP)
HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP) Description The French author George ...
- POJ 3978 Primes(求范围素数个数)
POJ 3978 Primes(求范围素数个数) id=3978">http://poj.org/problem? id=3978 题意: 给你一个区间范围A和B,要你求出[A,B]内 ...
- (KMP)Oulipo -- poj --3461
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=92486#problem/B http://poj.org/problem?id=3461 ...
- Match:Oulipo(POJ 3461)
Oulipo 题目大意:给你一个字符串,要你找到字符串包含指定子串的个数 只要你知道了KMP,这一题简直不要太简单,注意STL的string是会超时的,还是乖乖用char吧 #include < ...
- POJ 3461 kmp 应用
题意:求匹配串在文本中出现次数,KMP应用,理解了就OK了,每次匹配成功就累加次数,开始的时候超时, 由于在处理每次成功的时候让i=i-len2+1,相当于回溯了,后来一想,本次成功,相当于" ...
- AC日记——Oulipo poj 3461
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37958 Accepted: 15282 Description The ...
- POJ 3461 kmp
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40168 Accepted: 16135 Descript ...
- POJ 2752 (kmp求所有公共前后缀长度)
<题目链接> <转载于> 题目大意: 给出一个字符串str,求出str中存在多少子串,使得这些子串既是str的前缀,又是str的后缀.从小到大依次输出这些子串的长度.即输出该 ...
随机推荐
- 调皮捣蛋的Linux下有趣终端的合集
*本文作者:国光,转载自 FreeBuf.COM,原文地址:https://www.freebuf.com/news/144050.html 前言 刚开始接触Linux的我们,肯定认为Linux系统就 ...
- Pstools使用
pstool的介绍 PsTools是Sysinternals Suite中一款排名靠前的一个安全管理工具套件.现在被微软收购.目前pstools中含有12款各式各样的小工具.如果将它们灵活的运用,将会 ...
- tomcat单机多实例
catalina.home指向公用信息的位置,就是bin和lib的父目录. catalina.base指向每个Tomcat目录私有信息的位置,就是conf.logs.temp.webapps和work ...
- tp5 回滚事务记录,其中一条语句报错,全部回滚
#################################### 测试事务 // 启动事务 Db::startTrans(); try { //插入行为表 $data = [ 'userId' ...
- AnimationDrawable
①先定义一个AnimationDrawable的xml资源文件: <?xml version="1.0" encoding="utf-8"?> &l ...
- 详解用python实现简单的遗传算法
详解用python实现简单的遗传算法 今天整理之前写的代码,发现在做数模期间写的用python实现的遗传算法,感觉还是挺有意思的,就拿出来分享一下. 首先遗传算法是一种优化算法,通过模拟基因的优胜劣汰 ...
- http://www.cda.cn/view/25735.html
通过实例浅析Python对比C语言的编程思想差异 我一直使用 Python,用它处理各种数据科学项目. Python 以易用闻名.有编码经验者学习数天就能上手(或有效使用它). 听起来很不错,不过,如 ...
- JAVA 基础编程练习题17 【程序 17 猴子吃桃问题】
17 [程序 17 猴子吃桃问题] 题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个 第二天早上又 将剩下的桃子吃掉一半,又多吃了一个.以后每天早上都吃了前一天剩下的一 ...
- idea控制台中文乱码“淇℃伅”
方法一:找到tomcat的conf目录下的logging.properties,将输出到控制台的编码改为GBK java.util.logging.ConsoleHandler.encoding = ...
- Spring Aop(四)——基于Aspectj注解的Advice介绍
转发地址:https://www.iteye.com/blog/elim-2395315 4 基于Aspectj注解的Advice介绍 之前介绍过,Advice一共有五种类型,分别是before.af ...