HDU 1686 - Oulipo - [KMP模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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.
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.
should contain a single number, on a single line: the number of occurrences of
the word W in the text T.
题目大意:给出一个模式串和一个文本串,求文本串中模式串出现了几次。
解题思路:直接使用KMP算法。
#include<cstdio>
#include<cstring>
#include<iostream>
#define MAXpat 10000+5
#define MAXstr 1000000+5
using namespace std;
int Next[MAXpat];
char str[MAXstr],pat[MAXpat]; void getNext()
{
int i=, j=-, len=strlen(pat);
Next[]=-;
while(i<len)
{
if(j == - || pat[i] == pat[j]) Next[++i]=++j;
else j=Next[j];
}
}
int kmp()
{
int i=, j=, len1=strlen(str), len2=strlen(pat);
int ans=;
while(i<len1)
{
if(j == - || str[i] == pat[j]) i++, j++;
else j=Next[j];
if(j == len2) ans++;
}
return ans;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s%s",pat,str);
getNext();
printf("%d\n",kmp());
}
}
HDU 1686 - Oulipo - [KMP模板题]的更多相关文章
- Oulipo - HDU 1686 (KMP模板题)
题目大意:题目叙述很多,其实只看输入输出也能明白什么意思,给两个串W,T, 判断T串中包含几个串W. 分析:还是基础的KMP应用....................... 直接上代码. === ...
- HDU 1686 Oulipo kmp裸题
kmp算法可参考 kmp算法 汇总 #include <bits/stdc++.h> using namespace std; const int maxn=1000000+5; cons ...
- POJ Oulipo KMP 模板题
http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4 ...
- HDU - 1686 Oulipo KMP匹配运用
id=25191" target="_blank" style="color:blue; text-decoration:none">HDU - ...
- POJ Oulipo(KMP模板题)
题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #in ...
- Number Sequence - HDU 1711(KMP模板题)
题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1 分析:应该是最简单的模板题了吧..... 代码如下: ==================== ...
- 洛谷 P3375 【模板】KMP字符串匹配 || HDU 1686 Oulipo || kmp
HDU-1686 P3375 kmp介绍: http://www.matrix67.com/blog/archives/115 http://www.cnblogs.com/SYCstudio/p/7 ...
- hdu 1686 Oulipo kmp算法
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目: Problem Description The French author George ...
- hdu 1686 Oulipo KMP匹配次数统计
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 分析:典型的KMP算法,统计字符串匹配的次数. 用Next数组压缩时间复杂度,要做一些修改. / ...
随机推荐
- 解压安装的tomcat, 使用chkconfig命令让tomcat 随机启动,tomcat 变为系统服务
使用解压安装的tomcat包,命令行输入 service tomcat start 会报 tomcat: unrecognized service 错误提示,意思是说系统没有找到该服务. 好了,我们现 ...
- ios开发之--理解NSStringDrawingOptions每个选项的用法与意义
typedef NS_OPTIONS(NSInteger, NSStringDrawingOptions) { NSStringDrawingUsesLineFragmentOrigin = < ...
- lua 对表的简单序列化与反序列化
参考文档:http://blog.csdn.net/xiaodan007/article/details/7096718 function sz_T2S(_t) local szRet = " ...
- hadoop的Map阶段的四大步骤
深入理解map的几个阶段是怎样执行的.
- wordpress for sae
帮人建个站,准备用sae+wordpess,小研究一下 http://sae.sina.com.cn/?m=apps&a=detail&aid=1 http://wp4sae.org/ ...
- mysql相关攻击代码收集
1.批处理文件内容 @echo off net user li /add net user li /active:yes net localgroup Administrators li /add 2 ...
- CentOS6.5下安装Oracle11g
一.安装前系统准备 1. 修改主机名 #sed -i "s/HOSTNAME=localhost.localdomain/HOSTNAME=oracledb.01/" /etc/s ...
- MVC项目的简单总结
不能说是庆幸吧,正赶上公司要开发一个小小的MVC项目,于是这样任务就只有我自己承担,在这个项目中我才彻底感觉到自己之前有关MVC知识的无知,以为接触了皮毛就感觉能做项目,只要有心动脑思考就能完成一个完 ...
- php pear包打包方法
一)首先下载工具onion 浏览器打开,服务器上wget测试无法正常下载 地址:https://raw.github.com/c9s/Onion/master/onion 二)在临时目录下,建立相关目 ...
- Delphi中ClientDataSet的用法小结
Delphi中ClientDataSet的用法小结 TClientDataSet控件继承自TDataSet,其数据存储文件格式扩展名为 .cds,是基于文件型数据存储和操作的控件.该控件封装了对数据进 ...