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> ...
随机推荐
- [Android Pro] UI设计师不可不知的安卓屏幕知识
reference to : http://www.android100.org/html/201505/24/149342.html 不少设计师和工程师都被安卓设备纷繁的屏幕搞得晕头转向,我既做UI ...
- ip数据结构
本文摘自 linux kernel ip.h,感谢开源的GNU struct ip { #if __BYTE_ORDER == __LITTLE_ENDIAN unsigned int ip_hl:4 ...
- iOS开发网络篇—JSON介绍
一.什么是JSON JSON是一种轻量级的数据格式,一般用于数据交互 服务器返回给客户端的数据,一般都是JSON格式或者XML格式(文件下载除外) JSON的格式很像OC中的字典和数组 {" ...
- php 三元运算符使用说明和写法
PHP三元运算的2种写法代码实例 首先,我们现在看一个简单的例子: 代码如下: <?php //写法一: $a = 2; ($a == 1) ? $test = "我们" : ...
- PHP 文件上传类
FileUpload.; $]; $_newname = date(,). : To ...
- POJ1236 - Network of Schools tarjan
Network of Schools Time Limit: 1000MS Memory Limi ...
- 分享一下spark streaming与flume集成的scala代码。
文章来自:http://www.cnblogs.com/hark0623/p/4172462.html 转发请注明 object LogicHandle { def main(args: Array ...
- SU Demos-02Filtering-07Sumedian
不足之处,欢迎批评指正. 先看脚本内容: 脚本中用到的4个参数文件,是一系列x,t数据对,数组(x,t) 运行结果,
- 模拟 ACdream 1196 KIDx's Pagination
题目传送门 /* 简单模拟:考虑边界的情况输出的是不一样的,还有思维好,代码能短很多 */ #include <cstdio> #include <iostream> #inc ...
- HDU4862 Jump(放大边权的费用流)
题目大概给一个n×m的格子,每个格子有一个一位数字,格子不能重复经过,最多进行这样的k次行走:每一次选择任意一个格子出发,可以从当前格子走到下面或右边格子,花费能量是曼哈顿距离-1,而如果起点和终点格 ...