POJ 3461 kmp
| 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
#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
POJ 3461 kmp的更多相关文章
- POJ 3461 kmp 应用
题意:求匹配串在文本中出现次数,KMP应用,理解了就OK了,每次匹配成功就累加次数,开始的时候超时, 由于在处理每次成功的时候让i=i-len2+1,相当于回溯了,后来一想,本次成功,相当于" ...
- 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[附KMP算法详细流程讲解]
E - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 3461 Oulipo
E - Oulipo Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 3461 Oulipo(乌力波)
POJ 3461 Oulipo(乌力波) Time Limit: 1000MS Memory Limit: 65536K [Description] [题目描述] The French autho ...
- (KMP)Oulipo -- poj --3461
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=92486#problem/B http://poj.org/problem?id=3461 ...
- POJ 3461 Oulipo 【KMP统计子串数】
传送门:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submission ...
- POJ - 3461 (kmp)
题目链接:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissio ...
- POJ 3080 Blue Jeans、POJ 3461 Oulipo——KMP应用
题目:POJ3080 http://poj.org/problem?id=3080 题意:对于输入的文本串,输出最长的公共子串,如果长度相同,输出字典序最小的. 这题数据量很小,用暴力也是16ms,用 ...
随机推荐
- Awesome Flask Awesome
A curated list of awesome Flask resources and plugins Awesome Flask Framework Admin interface Authen ...
- kitti 数据集解析
1.KITTI数据集采集平台: KITTI数据采集平台包括2个灰度摄像机,2个彩色摄像机,一个Velodyne 3D激光雷达,4个光学镜头,以及1个GPS导航系统.坐标系转换原理参见click.KIT ...
- IO多路复用模型之epoll实现机制
设想一下如下场景:有100万个客户端同时与一个服务器进程保持着TCP连接.而每一时刻,通常只有几百上千个TCP连接是活跃的(事实上大部分场景都是这种情况).如何实现这样的高并发? 在select/po ...
- mysql 入门 1
连接mysql服务器 mysql -h localhost -u username -ppasswd 1.查看服务器存在的库 show databases; 2.创建数据库 create databa ...
- 算法导论17:摊还分析学习笔记(KMP复杂度证明)
在摊还分析中,通过求数据结构的一系列的操作的平均时间,来评价操作的代价.这样,即使这些操作中的某个单一操作的代价很高,也可以证明平均代价很低.摊还分析不涉及概率,它可以保证最坏情况下每个操作的平均性能 ...
- C - Bear and Five Cards
Description A little bear Limak plays a game. He has five cards. There is one number written on each ...
- SSE2 Intrinsics各函数介绍
http://blog.csdn.net/fengbingchun/article/details/18460199
- PYTHON3中 类的继承
继承 1:什么是继承 继承是一种创建新类的方式,在python中,新建的类可以继承一个或多个父类,也就是说在python中支持一个儿子继承多个爹. 新建的类成为子类或者派生类. 父类又可以成为基类或者 ...
- 51nod 1013【快速幂+逆元】
等比式子: Sn=(a1-an*q)/(1-q) n很大,搞一发快速幂,除法不适用于取膜,逆元一下(利用费马小定理) 假如p是质数,且gcd(a,p)=1,那么 a^(p-1)≡1(mod p).刚好 ...
- P4091 [HEOI2016/TJOI2016]求和(第二类斯特林数+NTT)
传送门 首先,因为在\(j>i\)的时候有\(S(i,j)=0\),所以原式可以写成\[Ans=\sum_{i=0}^n\sum_{j=0}^nS(i,j)\times 2^j\times j! ...