POJ 3461 Oulipo(KMP裸题)
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 给两个字符串,求第一个字符串在第二个中出现的次数
KMP最精华即next数组
参考博客:http://blog.csdn.net/zz_ylolita/article/details/50650391
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string.h>
using namespace std; char p[];
char s[];
int next[];
int lenp,lens; void getnext()
{
next[]=-;
int j=-;
for (int i=;i<lenp;i++)
{
while (j>- && p[j+]!=p[i]) j=next[j];
if (p[j+]==p[i]) j++;
next[i]=j;
}
}
int kmp()
{
int j=-,count=;
for (int i=;i<lens;i++)
{
while (j>- && p[j+]!=s[i]) j=next[j];
if (p[j+]==s[i]) j++;
if (j==lenp-)
{
count++;
j=next[j];
}
}
return count;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
cin>>p>>s;
lenp=strlen(p);
lens=strlen(s);
getnext();
printf("%d\n",kmp());
}
return true;
}
POJ 3461 Oulipo(KMP裸题)的更多相关文章
- POJ 3641 Oulipo KMP 水题
http://poj.org/problem?id=3461 直接KMP就好.水题 #include<cstdio> #include<cstring> const int M ...
- POJ 3080 Blue Jeans、POJ 3461 Oulipo——KMP应用
题目:POJ3080 http://poj.org/problem?id=3080 题意:对于输入的文本串,输出最长的公共子串,如果长度相同,输出字典序最小的. 这题数据量很小,用暴力也是16ms,用 ...
- [POJ] 3461 Oulipo [KMP算法]
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 23667 Accepted: 9492 Descripti ...
- POJ 3461 Oulipo KMP
题意:统计其中一个子串的出现次数 题解:即KMP算法中j==m的次数 //作者:1085422276 #include <cstdio> #include <cmath> #i ...
- POJ 3461 Oulipo KMP算法题解
本题就是给出非常多对字符串,然后问一个字符串在另外一个字符串出现的次数. 就是所谓的Strstr函数啦. Leetcode有这道差点儿一模一样的题目. 使用KMP算法加速.算法高手必会的算法了. 另外 ...
- POJ 3461 Oulipo KMP算法(模板)
题意: 给两组字符串a和b,求a在b中出现的次数 关于KMP: 马拉车算法是处理回文串,而KMP是处理前后缀的相同字符串的最长长度. a | a | b | a | a | f | a | a 数组 ...
- HDU 1686 Oulipo kmp裸题
kmp算法可参考 kmp算法 汇总 #include <bits/stdc++.h> using namespace std; const int maxn=1000000+5; cons ...
- POJ 3461 Oulipo(KMP,模式串在主串中出现次数 可重叠)
题意:给你两个字符串p和s,求出p在s中出现的次数. 显然,我们要先把模式串放到前面,之后主串放后面,中间隔开,这样就可以根据前缀数组的性质来求了. 我先想直接把p接到s前面,之后求Next数组对st ...
- 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 3461 Oulipo(乌力波)
POJ 3461 Oulipo(乌力波) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] The French autho ...
随机推荐
- ubuntu计划任务的编写
1,首先,我编写的计划任务是在ubunut系统上的.首先我们来认识一下每一个 * 这样子的符号代表什么意思吧! * * * * * 从左到右: 第1列表示分钟1-59 每分钟用*或者 */1表 ...
- Beta阶段——第1篇 Scrum 冲刺博客
第1篇 Scrum 冲刺博客 a. 介绍小组新加入的成员,Ta担任的角色. 新加入成员 郭炜埕 原先担任的角色 前端界面设计 现在担任的角色 前端开发,并协助后端开发 新加成员介绍 炜埕同学对界面设计 ...
- 将本地分支push到远程分支
git push origin [localbranch]:[remotebranch]
- 利用NPOI解析Excel的通用类
using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using NPOI. ...
- 启动Oracle时提示:ORA-01078:failure in processing system parameters
一.使用环境操作系统:CentOS release 6.2 (Final) 数据库:Oracle 12g数据库主目录:/ora12/product/product/12.1.0/db_1 二.问题描述 ...
- oracle 变量练习
select ename,&column2 from scott.emp; 输入sal: /替代标量: select * from &tab: select * from ...
- vue数组操作不更新视图问题
vue 观察数组的变异方法 更新视图 push() pop() shift() unshift() splice(i,n,arr) sort(xx) reverse() ex: app.book.pu ...
- 分析图第二讲导出图片和后期PS5.12
导出渲染的白模加上EXTRATEX.再导出一张“消隐”样式的模型图片.就是线稿图. 再导出一张着色显示图,并去掉边线.,用于后期PS选择范围用. 把这几张图全都导入ps. 渲染图的阴影面是灰色的,示例 ...
- jar包在控制台下运行
今天有个项目需要在控制台下面运行jar文件 流程 1 新建java项目 2 新建 HelloWorld.java public class HelloWorld { public static voi ...
- learning ddr Electrical Characteristics and AC Timing
reference: JEDS79-3F.pdf , page:181