hdu1686字符串kmp
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.
InputThe 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.
OutputFor 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
题意:还是找最大匹配,但是这次可以重复
题解:刚开始用的上一题的第一个方法,直接暴力遍历字符串结果tle,发现可以利用字符串kmp本身的特点处理
- #include<map>
- #include<set>
- #include<cmath>
- #include<queue>
- #include<stack>
- #include<vector>
- #include<cstdio>
- #include<iomanip>
- #include<cstdlib>
- #include<cstring>
- #include<iostream>
- #include<algorithm>
- #define pi acos(-1)
- #define ll long long
- #define mod 1000000007
- #define ls l,m,rt<<1
- #define rs m+1,r,rt<<1|1
- using namespace std;
- const double g=10.0,eps=1e-;
- const int N=+,maxn=+,inf=0x3f3f3f3f;
- int Next[N],slen,plen,res;
- string str,ptr;
- void getnext()
- {
- int k=-;
- Next[]=-;
- for(int i=;i<=slen-;i++)
- {
- while(k>-&&str[k+]!=str[i])k=Next[k];
- if(str[k+]==str[i])k++;
- Next[i]=k;
- }
- }
- void kmp()
- {
- int k=-;
- for(int i=;i<plen;i++)
- {
- while(k>-&&str[k+]!=ptr[i])k=Next[k];
- if(str[k+]==ptr[i])k++;
- if(k==slen-)
- {
- res++;
- }
- }
- }
- int main()
- {
- ios::sync_with_stdio(false);
- cin.tie();
- // cout<<setiosflags(ios::fixed)<<setprecision(2);
- int t;
- cin>>t;
- while(t--){
- cin>>str>>ptr;
- slen=str.size();
- plen=ptr.size();
- getnext();
- res=;
- kmp();
- cout<<res<<endl;
- }
- return ;
- }
hdu1686字符串kmp的更多相关文章
- hdu 5510 Bazinga(字符串kmp)
Bazinga Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- 模板—字符串—KMP(单模式串,单文本串)
模板—字符串—KMP(单模式串,单文本串) Code: #include <cstdio> #include <cstring> #include <algorithm& ...
- 字符串 --- KMP Eentend-Kmp 自动机 trie图 trie树 后缀树 后缀数组
涉及到字符串的问题,无外乎这样一些算法和数据结构:自动机 KMP算法 Extend-KMP 后缀树 后缀数组 trie树 trie图及其应用.当然这些都是比较高级的数据结构和算法,而这里面最常用和最熟 ...
- 【poj 3080】Blue Jeans(字符串--KMP+暴力枚举+剪枝)
题意:求n个串的字典序最小的最长公共子串. 解法:枚举第一个串的子串,与剩下的n-1个串KMP匹配,判断是否有这样的公共子串.从大长度开始枚举,找到了就break挺快的.而且KMP的作用就是匹配子串, ...
- 数据结构(复习)---------字符串-----KMP算法(转载)
字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串"BBC ABCDAB ABCDABCDABDE",我想知道,里面是否包含另一个字符串"ABCDABD" ...
- 字符串(KMP):BZOJ 3670 [Noi2014]动物园
3670: [Noi2014]动物园 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1521 Solved: 813[Submit][Status] ...
- HDU 4668 Finding string (解析字符串 + KMP)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题意:给出一个压缩后的串,以及一个模式串,问模式串 ...
- 流动python - 字符串KMP匹配
首先我们看一下简单的字符串匹配. 你可以把文本字符串s固定,模式字符串p从s对齐的左边缘,作为承担部分完全一致,匹配成功,失败将是模式字符串p整体向右1地点,继续检查对齐部分,重复. #朴素匹配 de ...
- 查找子字符串----KMP算法深入剖析
假设主串:a b a b c a b c a c b a b 子串:a b c a c 1.一般匹配算法 逐个字符的比较,匹配过程如下: 第一趟匹配 a b a b c a b c a c ...
随机推荐
- 最新版Intellij IDEA插件JRebel 7.0.7官方免费激活
本文转自:http://blog.csdn.net/u012283609/article/details/70213307 开场语 有时候真实比小说更加荒诞,因为虚构是在一定逻辑下进行的,而现实往往毫 ...
- Nginx能做什么
本文只针对Nginx在不加载第三方模块的情况能处理哪些事情,由于第三方模块太多所以也介绍不完,当然本文本身也可能介绍的不完整,毕竟只是我个人使用过和了解到过得.所以还请见谅,同时欢迎留言交流. Ngi ...
- 在IBM学到的东西,到底对我的程序生涯产生了多大的影响
我和很多人交流过一个有趣的现象,那就是刚毕业到30岁这段时间,会觉得时间过得很慢,总觉得自己还很年轻,但是一旦过了30岁,时间就如白驹过隙,一年又一年飞逝而过. 我自己也是,眼瞅着毕业快15年了,15 ...
- nginx作防盗链设置
盗链是一种损害原有网站合法权益,给原网站所在服务器造成额外负担的非法行为. 盗链的实现原理: 客户端向服务器请求资源时,为了减少网络带宽,提高响应时间,服务器一般不会一次将所有资源完整地传回给客户端. ...
- servlet+jsp+java实现Web应用
servlet+jsp+java实现Web应用 环境: 1,eclipse 2,tomcat3,eclipse tomcat 插件 开发过程: 1,建立一个Dynamic Web Project 2, ...
- oracle、Mysql数据库客户端DbVisualizer安装
原文链接:https://jingyan.baidu.com/article/454316ab675302f7a7c03a9e.html
- 20145302张薇 《网络对抗》MSF应用基础
20145302张薇 <网络对抗>MSF应用基础 实验内容 掌握metasploit的基本应用方式 1.主动攻击--ms08_067 2.针对浏览器的攻击--ms11_050 3.针对客户 ...
- cron表达式增加一段时间变为新的表达式
cron表达式是使用任务调度经常使用的表达式了.对于通常的简单任务,我们只需要一条cron表达式就能满足.但是有的时候任务也可以很复杂. 最近我遇到了一个问题,一条任务在开始的时候要触发A方法,在结束 ...
- cmder的使用和编码问题解决
cmder 是一款 windows 下的命令集合软件,它可以集合各种系统下的命令,并且操作非常快速方便.安装有两个版本,一个是简化版(4.27M),一个是完全版(75.7M),它们的唯一区别:完全版包 ...
- 'curl' is not recognized as an internal or external command
使用everything搜索本地的curl.exe发现如下 官网查看最新版本https://curl.haxx.se/windows/ 2019-03-06 最新版本7.64.0 curl-7.64. ...