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;
}
所以这是水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;
}
相关文章推荐
- POJ 3461 Oulipo(KMP:统计一个串出现的次数)
- poj 3461 Oulipo(KMP)
- HDU 1686 Oulipo , 同 POJ 3461 Oulipo (字符串匹配,KMP)
- poj 3461 Oulipo(KMP模板题)
- POJ 3461 Oulipo(简单KMP)
- poj 3461 Oulipo(KMP)
- POJ 3461 Oulipo (kmp入门)
- POJ---3461-Oulipo(KMP)
- POJ 3461 Oulipo [KMP模板题]
- POJ - 3461 Oulipo——kmp
- HDU 1686 Oulipo / POJ 3461 Oulipo / SCU 2652 Oulipo (字符串匹配,KMP)
- [KMP求模式在主串出现次数]POJ 3461 Oulipo
- poj 3461 Oulipo(KMP)
- POJ 3461 Oulipo (KMP)
- 【KMP】POJ 3461 Oulipo
- POJ 3461 Oulipo (kmp模板)
- POJ 3461 Oulipo【KMP】【模板题】(找出第一个字符串在第二个字符串中出现次数)
- 【POJ 3461】Oulipo(kmp)
- poj 3461 Oulipo kmp
- POJ 3461 - Oulipo(KMP)