您的位置:首页 > 其它

Poj 3461 Oulipo (KMP)

2015-03-21 16:57 429 查看
题目链接:poj 3461

所以这是水KMP模板?

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;

const int maxn=1000000+10;

int next[maxn];
char a[maxn];
char b[maxn];

void get_next(char *s){
int l=strlen(s);
int k=-1,i=0;
next[0]=-1;

while(i<l){
if(k==-1 || s[i]==s[k]){
i++; k++;
if(s[i]!=s[k])next[i]=k;
else next[i]=next[k];
}
else k=next[k];
}
}

int find(char *x,char *y){
get_next(x);
int lx=strlen(x),ly=strlen(y);
int i=0,j=0,tot=0;

while(j<ly){
if(i==-1 || x[i]==y[j]){
i++; j++;
if(i==lx)tot++;
}
else i=next[i];
}
return tot;
}

int main(){
int t; scanf("%d",&t);
while(t--){
scanf("%s%s",a,b);
printf("%d\n",find(a,b));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: