HDU 1686 Oulipo(优化的KMP)
Oulipo
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17441 Accepted Submission(s):
6938
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.
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.
BAPC
BAPC
AZA
AZAZAZA
VERDI
AVERDXIVYERDIAN
3
0
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
int next[], num;
char a[], b[];
int lena, lenb;
void get_next()//自身和自身比较,找某种特征
{
int i, j;
i = ; next[] = -; j = -;
while (i<lenb)
{
if (j == - || b[i] == b[j])//匹配成功的话,继续下一位
{
i++;
j++;
if (b[i] == b[j])
{
next[i] = next[j];
}
else
{
next[i] = j;
}
}
else//否则返回到可以匹配的位置
{
j = next[j];
}
}
}
void index_KMP()
{
int i, j;
i = ;
j = ;
num = ;
while (i<lena)
{
if (a[i] == b[j] || j == -) //进行两个字符串的匹配
{
i++;//匹配成功,往后继续匹配
j++;
}
else//匹配完成一次,代表出现了一次,记录下来
{
j = next[j];
}
if (j == lenb)//匹配失败,寻找数组中可以匹配的位置
{
num++;
j = next[j];
}
}
printf("%d\n", num);
}
int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
getchar();
while (n--)
{
gets(b);
gets(a);
lena = strlen(a);
lenb = strlen(b);
get_next();
index_KMP();
}
}
return ;
}
HDU 1686 Oulipo(优化的KMP)的更多相关文章
- 题解报告:hdu 1686 Oulipo(裸KMP)
Problem Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, w ...
- 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 ...
- HDU - 1686 Oulipo KMP匹配运用
id=25191" target="_blank" style="color:blue; text-decoration:none">HDU - ...
- hdu 1686 Oulipo KMP匹配次数统计
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 分析:典型的KMP算法,统计字符串匹配的次数. 用Next数组压缩时间复杂度,要做一些修改. / ...
- HDU 1686 - Oulipo - [KMP模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 Time Limit: 3000/1000 MS (Java/Others) Memory Li ...
- 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 题目大意:给两个字符串A,B求出A中出现了几次B(计算重复部分). 解题思路:稍微对kmp()函 ...
- hdu 1686 Oulipo (kmp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题目大意:寻找子链在母链中出现的次数. #include <iostream> #i ...
- HDU 1686 Oulipo , 同 POJ 3461 Oulipo (字符串匹配,KMP)
HDU题目 POJ题目 求目标串s中包含多少个模式串p KMP算法,必须好好利用next数组,, (kmp解析)——可参考 海子的博客 KMP算法 //写法一: #include<string ...
随机推荐
- K-Means & Sequential Leader Clustering
2017-12-31 19:08:37 k-平均算法源于信号处理中的一种向量量化方法,现在则更多地作为一种聚类分析方法流行于数据挖掘领域.k-means的目的是:把样本划分到k个聚类中,使得每个点都属 ...
- Linux下,EPM11.1.1.3 configurator 不能启动AdminServer
需要测试环境, 安装EPM11.1.1.3 到 CentOS 5.6 在运行configurator(/app/hyperion/common/config/9.5.0.0 时, 卡在[Startin ...
- ES5和ES6中的继承
看到一篇写的非常好的关于js继承的文章,其中对构造函数.原型.实例之间的关系的描述十分透彻,故转载作者文章以随时学习,并供大家共同进步! ES5 ES5中的继承,看图: function Super( ...
- Credentials(Rails5.2新) 很基础的知识点,具体还要实操。
Credentials(Rails5.2新) 增加config/credentials.yml.enc 憎加config/master.key 移除了5.1使用的config/secrets.yml, ...
- HIVE之常用字符串函数
可以参考: 博文 : https://www.iteblog.com/archives/1639.html
- 解决Myeclipse闪退问题
才安装好Myeclipse就出了问题,打开之后没过多久就自动退出了,看了好多解决方法都无效,后来才找到正确路径,转载过来方便跟我遇到同样问题的小伙伴,尽快解决 转载自:http://blog.csdn ...
- BZOJ1555 KD之死
如果没有必选的限制条件,就是水题了... 只要按照w + t排序就可以了,然后搞个堆来维护 于是有了限制条件,还是水题... 到了必选的时候强制选上,不加入堆中即可. /*************** ...
- 记c#中构造函数中this的用法
今天谈及这个话题,是因为在看别人代码,遇到的.本着知其然还要知其所以然的精神,在网上大肆查找了一遍.得到了一些答案.并在此做一个简单的笔记,以便自己和他人以后查找,与我一同成长进步. 在C#中this ...
- Double H4.0
Double H4.0 修改完善已提交的需求规格说明书 https://docs.qq.com/doc/DTGxWRkh6c3ZLVldq?tdsourcetag=s_pcqq_file_edit&a ...
- block 回调个人理解
在网上见过这么个面试题 使用block和GCD时要注意些什么?他们是一回事吗?block在ARC和MRC的用法有什么不同?使用时要注意些什么? 首先block 和 GCD 在我看来他们是完全不同的概念 ...