POJ_3460_Oulipo_KMP

来源:互联网 发布:公司域名查询 编辑:程序博客网 时间:2024/06/11 14:41
#include <iostream>#include <cstdio>#include <string.h>#define MAX 10010#define MAXM 1000010using namespace std;int next[MAX];void getNext(char w[]){    next[0] = -1;    next[1] = 0;    int i = 0, j = -1;    while (i < strlen(w))    {        if (j == -1 || w[j] == w[i])            next[++i] = ++j;        else            j = next[j];    }}int main(){    //freopen("aa.txt", "r", stdin);    char w[MAX], t[MAXM];    int n;    scanf("%d", &n);    while (n--)    {        scanf("%s", w);        scanf("%s", t);        getNext(w);        int len1 = strlen(t), len2 = strlen(w);        int count = 0, j = 0, i = 0;        while (i < len1)        {            if (j == -1 || w[j] == t[i])            {                i++;                j++;            }            else                j = next[j];            if (j == len2)            {                count++;                j = next[j];            }        }        printf("%d\n", count);    }}