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开启慢日志
ubuntu 开启mysql日志记录 1.找到mysql的配置文件sudo vim /etc/mysql/my.cnf将下面两行的#去掉#general_log_file = /var/log/mys ...
- Parse error: syntax error, unexpected end of file in * 的解决办法
这个原因很简单,就是你的php语法错误. 在你的php代码种出现了<? ?> 标准的是<?php ?>
- centos命令行系列之centos查看磁盘空间大小
df -h 扩展: 1.查看当前文件夹所有文件大小 du -sh 2.查看指定文件下所有文件大小 du -h /data/ 3.查看指定文件大小 du -h install.log 4.查指定文件夹大 ...
- springboot activiti 配置项详解
asyncExecutorEnabled属性设置设置true后将代替那些老的Job executorspring.activiti.async-executor-enabled=false sprin ...
- python assert使用说明
python assert断言的作用 python assert断言是声明其布尔值必须为真的判定,如果发生异常就说明表达示为假. assert断言语句的语法格式 判断a与1.b是否一致,msg类似备注 ...
- [洛谷 P2508] 圆上的整点
题目描述 求一个给定的圆(x^2+y^2=r^2),在圆周上有多少个点的坐标是整数. 输入输出格式 输入格式: r 输出格式: 整点个数 输入输出样例 输入样例#1: 4 输出样例#1: 4 说明 n ...
- linux 逻辑卷管理 /dev/mapper/VolGroup-lv_root 100%调整分区大小
1.解决过程 # df -h // 查看分区 # umount /home // 取消挂载 # e2fsck -f /dev/mapper/VolGroup-lv_home // 分区检测 ...
- spring多个AOP执行先后顺序(面试问题:怎么控制多个aop的执行循序)
转载:spring多个AOP执行先后顺序(面试问题:怎么控制多个aop的执行循序) 众所周知,spring声明式事务是基于AOP实现的,那么,如果我们在同一个方法自定义多个AOP,我们如何指定他们的执 ...
- Vue--项目开发之实现tabbar功能来学习单文件组件1
创建好一个Vue项目后,我们进入项目里,点开src文件下的components文件里的helloworld.vue 文件.清空初始数据.然后开始编写. 一个.vue文件初始格式为以下三部分(组件三部曲 ...
- Shell编程积累 zhuan
在新的shell里执行程序 cd /home/lq/Server/anew-lstm_scriptmatlab -nodesktop -singleCompThred -r 'aStart' ,qui ...