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,用 ...
随机推荐
- JavaScript-Tool-富文本:Simditor
ylbtech-JavaScript-Tool-富文本:Simditor 1.返回顶部 1. 2. 2.返回顶部 1. Simditor 是团队协作工具 Tower 使用的富文本编辑器. 相比传统的编 ...
- 使用 DDMenuController 类的方法(非常好用的抽屉类)
关于使用 DDMenuController 类的方法笔记:参考 DDMenuController 是一款非常好用的抽屉类文件. #pragma mark - 界面配置左右导航条的按钮 //[self ...
- 运用Eclipse的Working Set,界面清爽多了
使用Eclipse的Working Set,界面清爽多了 想必大家的Eclipse里也会有这么多得工程...... 每次工作使用到的项目肯定不会太多...... 每次从这么大数量的工程当中找到自己要使 ...
- SpannableStringBuilder 用法浅析以及仿陌陌表情
SpannableStringBuilder 官方文档解释:这个类可以使文本的内容和标记都可以改变.当我们要为TextView或者Edittext里面的文字加入加入一些效果,如下划线,颜色标 识,超 ...
- STL树
前几天觉得STL中没有树和图真是一种莫大的遗憾啊,但是在网上搜了搜,发现其实可以用容器很简单的构造树. 还是废话少说上代码: struct TreeNode{ DataType data; // Da ...
- LeetCode: 496 Next Greater Element I(easy)
题目: You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset ...
- TP5之发送邮件
1.下载扩展,vendor\phpmailer 文件结构: 2.话不多说,上代码 注意点: · 需要提前开通对应邮箱的SMTP服务 · $mail->Host = " & ...
- C++内存泄漏检测
CRT检测 定位内存泄漏位置 #include "stdafx.h" #ifdef _DEBUG #define DEBUG_NEW new( _NORMAL_BLOCK, __F ...
- Go语言之父谈Go:大道至简
http://www.csdn.net/article/2012-07-05/2807113-less-is-exponentially-more 摘要:导读:这篇文章是Google首席工程师.Go语 ...
- Educational Codeforces Round 18D(完全二叉树中序遍历&lowbit)
题目链接:http://codeforces.com/contest/792/problem/D 题意:第一行输入n, q,分别表示给出一颗n个节点的中序遍历满二叉树,后面有q个询问; 接下来有q组形 ...