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);
}
}

  

时间: 2024-12-20 20:44:19

HDU 2087 kmp模板题的相关文章

hdu 1711 KMP模板题

// hdu 1711 KMP模板题 // 贴个KMP模板吧~~~ #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; const int MAX_N = 1000008; const int MAX_M = 10008; int T[MAX_N]; int p[MAX_M]; int f[MAX_M]; int

hdu 1686 KMP模板

1 // hdu 1686 KMP模板 2 3 // 没啥好说的,KMP裸题,这里是MP模板 4 5 #include <cstdio> 6 #include <iostream> 7 #include <cstring> 8 #include <algorithm> 9 10 using namespace std; 11 12 const int MAX_N = 1000008; 13 const int MAX_M = 10008; 14 char T

Saving Princess claire_(hdu 4308 bfs模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2305    Accepted Submission(s): 822 Problem Description Princess claire_ wa

POJ Oulipo(KMP模板题)

题意:找出模板在文本串中出现的次数 思路:KMP模板题 #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #include<map> #include<queue> #include<stack&

hdu 2586 LCA模板题(离线算法)

http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B&quo

poj 3461 Oulipo(KMP模板题)

题目链接:http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 23559   Accepted: 9437 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a

Number Sequence - HDU 1711(KMP模板题)

题意:给你一个a串和一个b串,问b串是否是a串的子串,如果是返回b在a中最早出现的位置,否则输出-1 分析:应该是最简单的模板题了吧..... 代码如下: ============================================================================================== #include<stdio.h> #include<string.h> const int MAXM = 1e4+7; const int

POJ Oulipo KMP 模板题

http://poj.org/problem?id=3461 Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41051   Accepted: 16547 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a mem

hdu 2087 剪花布条 kmp模板题

也是kuangbin专题的 专题名字太长 不复制了…… 刚好数据结构也学了kmp 找一道题敲敲模板…… 暴力的字符串匹配是O(n*m)的时间复杂度 而kmp通过一个O(m)的预处理将字符串匹配的时间复杂度降到了O(n+m) kmp的核心是next数组的处理和利用next数组进行字符串匹配 这两个理解了就会用kmp了 1 /* *********************************************** 2 Author :Sun Yuefeng 3 Created Time :