poj 3461 Oulipo kmp字符串匹配

//#include 
#include 
#include 

using namespace std;

//string a,b;
char a[10000],b[1000000];
int asize,bsize;
int kmp(){
    int *pi = new int [asize];
    pi[0] = -1;
    for(int i = 1,k = -1;iwhile(k>-1&&a[k+1] != a[i])
            k = pi[k];
        if(a[k+1] == a[i])
            k++;
        pi[i] = k;
    }
    int result = 0;
    for(int i = 0,k = -1;iwhile(k>-1&&a[k+1] != b[i])
            k = pi[k];
        if(a[k+1] == b[i])
            k++;
        if(k == asize - 1){
            result++;
            k = pi[k];
        }
    }
    return result;
}

int main(){
    int times;
    //cin >> times;
    scanf("%d",×);
    while(times--){
        scanf("%s%s",a,b);
        asize = strlen(a);
        bsize = strlen(b);
        printf("%d\n",kmp());
        //a.clear();
        //b.clear();
        //cin >> a >> b;
        //cout << kmp() << endl;
    }
    return 0;
}

你可能感兴趣的:(数据结构与算法)