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,用 ...
随机推荐
- 合并多个cv::Mat类型,即合并多个图片的接口
1. cv::Mat get_merage_image(cv::Mat cur_frame) { cv::Mat image_one=cur_frame; cv::Mat image_two=cur_ ...
- Httpclient爬取优酷网
参考:http://www.cnblogs.com/lchzls/p/6277210.html /httpClient/src/main/java/com/louis/youku/Page.java ...
- Tomcat之the jre_home environment variable is not defined correctly this environment variable is need
参考https://blog.csdn.net/qq_30507287/article/details/53981851 今天在服务器的tomcat上部署.war文件,双击startup闪退,然后在t ...
- Identity Server 4 原理和实战(完结)_----选看 OAuth 2.0 简介(下)
https://www.yuque.com/yuejiangliu/dotnet/asu0b9 端点 Endpoint Authorization Endpoint,授权端点 在浏览器里面和用户交互 ...
- mysql:视图,触发器
一视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL 语句获取动态的数据集,并未其命名],用户使用时只需使用名称即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以吧查询过程的临时表 ...
- jQuery 如何获取ASP.NET服务器控件的值
由于ASP.NET网页运行后,服务器控件会随机生成客户端id,jquery获取时候不太好操作,总结有以下3种方法: 服务器控件代码:<asp:TextBox ID="txtUserID ...
- 洛谷 - P3786 - 萃香抱西瓜 - 状压dp
重构一下就过了,不知道之前错在哪里. #include<bits/stdc++.h> using namespace std; typedef unsigned long long ull ...
- Unity3D中常用的数据结构总结与分
阅读目录 1.几种常见的数据结构 2.几种常见数据结构的使用情景 来到周末,小匹夫终于有精力和时间来更新下博客了.前段时间小匹夫读过一份代码,对其中各种数据结构灵活的使用赞不绝口,同时也大大激发了小匹 ...
- Codevs 1312 连续自然数和
1312 连续自然数和 题目描述 Description 对于一个自然数M,求出所有的连续的自然数段,使得这些连续自然数段的全部数字和为M.eg:1998+1999+2000+2001+2002=10 ...
- P3270 [JLOI2016]成绩比较(拉格朗日插值)
传送门 挺神仙的啊-- 设\(f[i][j]\)为考虑前\(i\)门课程,有\(j\)个人被\(B\)爷碾压的方案数,那么转移为\[f[i][j]=\sum_{k=j}^{n-1}f[i-1][k]\ ...