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模板题的更多相关文章

  1. HDU 1711 - Number Sequence - [KMP模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

  2. hdu 2087 kmp

    http://acm.hdu.edu.cn/showproblem.php?pid=2087 算是模板题吧,找到一个子串之后将模板串指针归零否则会重复计算. #include<bits/stdc ...

  3. hdu 1686 KMP模板

    // hdu 1686 KMP模板 // 没啥好说的,KMP裸题,这里是MP模板 #include <cstdio> #include <iostream> #include ...

  4. Oulipo HDU 1686 KMP模板

    题目大意:求模式串在主串中的出现次数. 题目思路:KMP模板题 #include<iostream> #include<algorithm> #include<cstri ...

  5. POJ Oulipo KMP 模板题

    http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4 ...

  6. POJ Oulipo(KMP模板题)

    题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #in ...

  7. HDU 2087  KMP模板题

    1.HDU 2087 2.题意:一个主串,一个子串,求子串在主串里出现了几次. 3.总结:看了题解,还是不太懂.. //#include<iostream>#include<cmat ...

  8. Number Sequence - HDU 1711(KMP模板题)

    题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1   分析:应该是最简单的模板题了吧..... 代码如下: ==================== ...

  9. hdu 1711 Number Sequence(KMP模板题)

    我的第一道KMP. 把两个数列分别当成KMP算法中的模式串和目标串,这道题就变成了一个KMP算法模板题. #include<stdio.h> #include<string.h> ...

随机推荐

  1. SVN常见错误和版本冲突解决

    之前在Eclipse下面误删除了svn的一些插件包,后来重装了就问题重重,在这里还是建议, Windows下SVN最好使用桌面版,在文件管理器下面更新和提交. 1.常见错误整理 #, c-format ...

  2. Visual Studio 2012 使用免费的Team Foundation Service(转载)

    转载地址:http://www.cnblogs.com/chsword/archive/2012/12/14/visualstudio2012_tfs.html VS2012提供了在线的TFS服务,免 ...

  3. 怎么判定一个mac地址是multicast还是unicast.

    MAC地址是以太网二层使用的一个48bit(6字节十六进制数)的地址,用来标识设备位置.MAC地址分成两部分,前24位是组织唯一标识符(OUI, Organizationally unique ide ...

  4. [转]C# Winform ListView使用

    以下内容均来自网上,个人收集整理,具体出处也难确认了,就没写出处了: 一.基本使用: listView.View = View.Details;//设置视图 listView.SmallImageLi ...

  5. [译]C#控制管理VisualSVN Server

    VisualSVN Server可以用WMI接口管理(Windows Management Instrumentation). VisualSVN Server安装的计算机中,位于%VISUALSVN ...

  6. Java Hour 43 Weather ( 12 ) - 杭州 Show

    终于从fastjson 的阴影中走出来了,接下去就是显示一个完整的天气信息了. 43.1 Weather Show 首先增加Model 中的属性. public class Weatherinfo { ...

  7. 为什么是List list = new ArrayList() 而不直接用ArrayList

    为什么是List list = new ArrayList(),而不直接用ArrayList? 编程是要面向对象编程,针对抽象(接口),而非具体.List 是接口,ArrayList是实现. 实现Li ...

  8. Activity启动方式

    Activity启动方式有四种,分别是: standard singleTop singleTask singleInstance 可以根据实际的需求为Activity设置对应的启动模式,从而可以避免 ...

  9. hdu 1455 Sticks

    Sticks Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  10. C Golden gun的巧克力

    Time Limit:1000MS  Memory Limit:65535K 题型: 编程题   语言: 无限制 描述 众所周知,13级有尊大神Golden gun,人称根叔,简称金枪!众立志进校队的 ...