HDU 1686 Oulipo kmp模板

来源:互联网 发布:广州经济开发区知乎 编辑:程序博客网 时间:2024/06/05 02:03

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1686

题意:给一个模式串,一个目标串,问目标串中有几个模式串

思路:kmp模板题,留个模板。。。

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 10010;char s1[N], s2[N*100];int nt[N];void getnt(){    int i = 0, j = -1;    nt[0] = -1;    while(s1[i])    {        if(j == -1 || s1[i] == s1[j])        {            i++, j++;            if(s1[i] != s1[j]) nt[i] = j;            else nt[i] = nt[j];        }        else j = nt[j];    }}int kmp(){    int i = 0, j = 0, cnt = 0;    getnt();    while(s2[i])    {        if(j == -1 || s2[i] == s1[j])            i++, j++;        else j = nt[j];        if(j != -1 && ! s1[j])            cnt++, j = nt[j];    }    return cnt;}int main(){    int t;    scanf("%d", &t);    while(t--)    {        scanf(" %s %s", s1, s2);        printf("%d\n", kmp());    }    return 0;}


0 0
原创粉丝点击