HDU1686 KMP模板

  仍旧是裸的字符串匹配可以拿来熟悉下字符串匹配问题,我是用来熟悉KMP的。

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N = 1e6+5;
const int M = 1e4+5;

char txt[N] , pat[M];
int pf[M];

void CPF()
{
    pf[1] = 0;
    int k = 0;
    int len = strlen(pat+1);
    for (int i=2;i<=len;i++)
    {
        while (k && pat[k+1]!=pat[i])
            k = pf[k];
        if (pat[k+1]==pat[i])
            k++;
        pf[i] = k;
    }
}

int KMP()
{
    int k = 0 , ans=0;
    int lent = strlen(txt+1) , lenp = strlen(pat+1);
   // cout << lent << " " << lenp << endl;
    for (int i=1;i<=lent;i++)
    {
        while (k && txt[i]!=pat[k+1])
            k = pf[k];
        if (pat[k+1]==txt[i])
            k++;
        if (k==lenp)
        {
            ans++;
        }
    }
    return ans;
}
时间: 2024-12-28 17:00:26

HDU1686 KMP模板的相关文章

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

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

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

剪花布条---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

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

数据结构实验之串三:KMP应用(KMP模板)

数据结构实验之串三:KMP应用(KMP模板) AC_Code: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <map> 6 #include <stack> 7 using namespace std; 8 typedef long long ll; 9 int Nex[1000000]; 10

扩展KMP模板

扩展KMP:    给出模板串A和子串B,长度分别为lenA和lenB,要求在线性时间内,对于每个A[i](0 <= i < lenA),求出A[i..lenA-1]与B的最长公共前缀长度,记为ex[i](或者说,ex[i]为满足A[i..i + z - 1]==B[0 .. z - 1]的最大的z值).    扩展KMP可以用来解决很多字符串问题,如求一个字符串的最长回文子串和最长重复子串.[算法]    设next[i]为满足B[i..i + z - 1] == B[0..z - 1]的最