1. 程式人生 > >Oulipo POJ - 3461 KMP

Oulipo POJ - 3461 KMP

ring 分類 clu rst con gets amp spa open

//以前沒分類過 現在該分類一下了
#include <iostream> #include <algorithm> #include <stdio.h> #include <set> #include <queue> #include <cstring> #include <string> #include <limits.h> #include <string.h> #include <vector> #include <map> #define LL long long #define
INF 2100000000 #define fi first #define se second #define lowbit(x) (x&(-x)) #define eps 5e-7 using namespace std; const int maxn=(int)1e6 +10; const int maxm=(int)1e4+10; const LL MOD=(LL)1e9+7; int next1[maxm]; void kmppre(char x[]) { int i,j; j=next1[0]=-1; //next[i]表示在第i個字符失配後, //模式串中0->next[i]處仍為匹配狀態 i=0
; while(x[i]) { while(j!=-1&&x[i]!=x[j]) j=next1[j]; next1[++i]=++j; } } int kmpcount(char x[],char y[]){ int i,j,ans=0; kmppre(x); i=j=0; while(y[i]){ while(j!=-1&&y[i]!=x[j])j=next1[j]; i++;j++; if(!x[j]){ ans++;j=next1[j]; } } return ans; } char s1[maxm],s2[maxn]; int main() { #ifdef shuaishuai freopen(
"C:\\Users\\hasee\\Desktop\\a.txt","r",stdin); //freopen("C:\\Users\\hasee\\Desktop\\b.txt","w",stdout); #endif int t; scanf("%d\n", &t); while(t--){ gets(s1);gets(s2); printf("%d\n",kmpcount(s1,s2)); } return 0; }

Oulipo POJ - 3461 KMP