您的位置:首页 > 其它

hdoj 1686 Oulipo

2017-05-30 22:25 211 查看
题目链接:Oulipo

题目大意:给你一个模式串和原串,计算模式串在原串里面出现的次数

题目思路:直接KMP就好

#include <bits/stdc++.h>

using namespace std;
const int maxn = 1e6+10;

int n,Next[maxn];
char mo[maxn],str[maxn];

int main(){
scanf("%d",&n);
while(n--){
memset(Next,-1,sizeof(Next));
scanf("%s%s",mo,str);
int i = 0,j = -1,len = strlen(mo);
while(i < len){
if(j == -1||mo[i] == mo[j]) Next[++i] = ++j;
else j = Next[j];
}
int ans = 0,len1 = strlen(str);
i = j = 0;
while(i < len1){
if(j == -1||str[i] == mo[j]) ++i,++j;
else j = Next[j];
if(j == len) ans++;
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: