【KMP模板】简单写个KMP~

本来easy的KMP

却一直过不了洛谷的模板题。。。

仔细一看原来在输出next数组时打的回车而不是空格。。。

身败名裂。。。

话说有个sunday貌似一般状况下比KMP快呢。。。去看看2333

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

char a[1000010],b[10010];
int nextt[10010];

int main()
{
    scanf("%s%s",a+1,b+1);
    int la=strlen(a+1);
    int lb=strlen(b+1);
    int j=0;
    for(int i=2;i<=lb;i++)
    {
        while(b[j+1]!=b[i]&&j) j=nextt[j];
        j+=(b[j+1]==b[i]);
        nextt[i]=j;
    }

    j=0;
    for(int i=1;i<=la;i++){
        while(j&&a[i]!=b[j+1]) j=nextt[j];
        j+=(a[i]==b[j+1]);
        if(j==lb) printf("%d\n",i-lb+1);
    }
    for(int i=1;i<=lb;++i)
        printf("%d ",nextt[i]);
    return 0;
}
时间: 2024-10-06 00:28:53

【KMP模板】简单写个KMP~的相关文章

KMP 算法简单解释

KMP 算法简单解释 ? 讲KMP算法,离不开BF,实际上,KMP就是BF升级版,主要流程和BF一样,就是在削除回溯上花了点功夫,利用Next数组来削除 <( ̄︶ ̄)[GO!] 1. 先看看BF算法(暴力破解) int Brute_force_1(const char *S, const char *T) { if (!S || !T) return -1; int lenS = strlen(S); int lenT = strlen(T); int i = 0; //主串下标索引 int j

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

KMP算法的定义及KMP练手题 HDU 1711 Number Sequence (我的模板代码)

题意:就是要你来找出b数组在a数组中最先匹配的位置,如果没有则输出-1 思路:直接KMP算法(算法具体思想这位牛写的不错http://blog.csdn.net/v_july_v/article/details/7041827) AC代码: #include<cstdio> #include<cstring> #include<stdlib.h> #include<iostream> using namespace std; #define maxn 100

HDU 1711 Number Sequence(KMP模板)

http://acm.hdu.edu.cn/showproblem.php?pid=1711 这道题就是一个KMP模板. 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 const int maxn = 1000000+5; 6 7 int n,m; 8 9 int next[maxn]; 10 int a[maxn], b[maxn]; 11 12 void get_next() 13 { 1

POJ Oulipo(KMP模板题)

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

[POJ 3461] Oulipo &amp; KMP模板

Oulipo Time Limit: 1000ms, Memory Limit: 65536K Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote from the book: Tout avait Pair normal, mai

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

(KMP 1.1)hdu 1711 Number Sequence(KMP的简单应用——求pattern在text中第一次出现的位置)

题目: Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12902    Accepted Submission(s): 5845 Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1

剪花布条---hdu2087(kmp模板)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2087 kmp模板题: #include <cstdio> #include <cstring> #include <iostream> using namespace std; #define N 1100 char s1[N], s2[N]; int p[N], L1, L2; void Getp() { int i=0, j=-1; p[0] = -1; while(i