HDU 2087 kmp模板题
s为主串 t为模板串 求t的nextt 加const
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<math.h>
#include<queue>
using namespace std;
char s[1005];
char t[1005];
int nextt[1005];
void makenext(const char t[])
{
int i=0,j=-1;
int len=strlen(t);
nextt[0]=-1;
while(i<len)
{
if(j==-1||t[i]==t[j])
{
i++;
j++;
nextt[i]=j;
}
else j=nextt[j];
}
}
int ans;
void kmp(const char s[],const char t[])
{
makenext(t);
int len1=strlen(s);
int len2=strlen(t);
int i=0,j=0;
while(i<len1)
{
if(j==-1||s[i]==t[j])
{
i++;
j++;
}
else j=nextt[j];
if(j==len2)
{
ans++;
j=0;
}
}
}
int main(){
while(~scanf("%s",s))
{
if(strcmp(s,"#")==0)
break;
scanf("%s",t);
ans=0;
kmp(s,t);
printf("%d\n",ans);
}
}
HDU 2087 kmp模板题的更多相关文章
- HDU 1711 - Number Sequence - [KMP模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...
- hdu 2087 kmp
http://acm.hdu.edu.cn/showproblem.php?pid=2087 算是模板题吧,找到一个子串之后将模板串指针归零否则会重复计算. #include<bits/stdc ...
- hdu 1686 KMP模板
// hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include ...
- Oulipo HDU 1686 KMP模板
题目大意:求模式串在主串中的出现次数. 题目思路:KMP模板题 #include<iostream> #include<algorithm> #include<cstri ...
- POJ Oulipo KMP 模板题
http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4 ...
- POJ Oulipo(KMP模板题)
题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #in ...
- HDU 2087 KMP模板题
1.HDU 2087 2.题意:一个主串,一个子串,求子串在主串里出现了几次. 3.总结:看了题解,还是不太懂.. //#include<iostream>#include<cmat ...
- Number Sequence - HDU 1711(KMP模板题)
题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1 分析:应该是最简单的模板题了吧..... 代码如下: ==================== ...
- hdu 1711 Number Sequence(KMP模板题)
我的第一道KMP. 把两个数列分别当成KMP算法中的模式串和目标串,这道题就变成了一个KMP算法模板题. #include<stdio.h> #include<string.h> ...
随机推荐
- Tomcat AccessLog 格式化
有的时候我们要使用日志分析工具对日志进行分析,需要对日志进行格式化,比如,要把accessLog格式化成这样的格式 c-ip s-ip x-InstancePort date time-taken x ...
- ListView滑动删除效果实现
通过继承ListView然后结合PopupWindow实现 首先是布局文件: delete_btn.xml:这里只需要一个Button <?xml version="1.0" ...
- WPF MVVM 关闭View
在ViewModel中定义一个变量: private Action _closeAction; 在ViewModel的构造函数中这样定义:public MainWindowViewModel(Acti ...
- poj 3517 约瑟夫环
最简单的约瑟夫环,虽然感觉永远不会考约瑟夫环,但数学正好刷到这部分,跳过去的话很难过 直接粘别人分析了 约瑟夫问题: 用数学方法解的时候需要注意应当从0开始编号,因为取余会等到0解. 实质是一个递推, ...
- android ndk环境配置(转)
转载自:http://jingyan.baidu.com/article/3ea51489e7a9bd52e61bbac7.html android sdk 更新到 r23 时,eclipse 自带 ...
- 几种php 删除数组元素方法
几种php教程 删除数组元素方法在很多情况下我们的数组会出现重复情况,那我们删除数组中一些重复的内容怎么办,这些元素我必须保持他唯一,所以就想办法来删除它们,下面利用了遍历查询来删除重复数组元素的几种 ...
- helpDB
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Da ...
- B 倒不了的塔
Time Limit:1000MS Memory Limit:65535K 题型: 编程题 语言: 无限制 描述 Dota是Defense of the Ancients的简称,是一个dhk和y ...
- 阿里云ECS(云服务器)之产品简介
参考阿里产品文档:https://docs.aliyun.com/?spm=5176.100054.3.1.ywnrMX#/pub/ecs/product-introduction/concept
- EntityFramework 6.0< Code First > 连接 Mysql数据库
网上有很多关于用EntityFrame来连接Mysql数据库的教程,可是很多并不靠谱,转载的太多了.找了很久,总算是配置好了,现在分享一下. 一,安装: 1.开发环境: VS2013与EF6 ...