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. 有哪些关于 Android 开发的博客值得订阅?

    链接:http://www.zhihu.com/question/19788650/answer/60771437来源:知乎 Google 官方[Android Developers Blog](An ...

  2. Type InvokeMember()用法简介

    举例: Type tDate = typeof(System.DateTime); Object result = tDate.InvokeMember("Now", Bindin ...

  3. THINKPHP 默认模板路径替换

    APP_PATH // 当前项目目录APP_NAME // 当前项目名称 ACTION_NAME // 当前操作名称 CACHE_PATH // 项目模版缓存目录 CONFIG_PATH //项目配置 ...

  4. php怎么判断网页是电脑访问还是手机访问

    .第一种方法 <?php  function check_wap() {       ) {           ;$i<count($list);$i++){         ){    ...

  5. php短信发送

    <?php   ) {             );         curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_content);         cu ...

  6. Android R文件相关问题

        今天遇到的问题,gen下没有自动生成文件,而大部分java文件中错误是找不到R.java.“R cannot be resolved to a variable” 这就一定有别的原因造成错误, ...

  7. Sql Server REPLACE函数的使用

    REPLACE用第三个表达式替换第一个字符串表达式中出现的所有第二个给定字符串表达式. 语法REPLACE ( ''string_replace1'' , ''string_replace2'' , ...

  8. Spring计划

    团队: 郭志豪:http://www.cnblogs.com/gzh13692021053/ 杨子健:http://www.cnblogs.com/yzj666/ 刘森松:http://www.cnb ...

  9. BZOJ3853 : GCD Array

    1 n d v相当于给$a[x]+=v[\gcd(x,n)=d]$ \[\begin{eqnarray*}&&v[\gcd(x,n)=d]\\&=&v[\gcd(\fr ...

  10. Underscore.js 中 _.throttle 和 _.debounce 的差异

    两个方法都是用来控制事件的频率的,在mousemove,resize等这种高频率触发事件中,控制其响应频率可以明显提高程序的流畅性,减少资源的占用. 通过分析其源代码: _.throttle方法源码 ...