Oulipo
题目链接:
http://poj.org/problem?id=3461
解题思路:
字符串匹配。。。kmp即可快速求解。。。
AC代码:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
int Next[10005];
char word[10005],text[1000005];
void getnext(){
int j = 0,k = -1;
int l = strlen(word);
Next[0] = -1;
while(j < l){
if(k == -1 || word[j] == word[k]){
j++;k++;
Next[j] = k;
}
else
k = Next[k];
}
}
void kmp(){
getnext();
int l1 = strlen(text),l2 = strlen(word);
int i = 0,j = 0,sum = 0;
while(i < l1){
if(j == -1 || text[i] == word[j]){
i++;
j++;
}
else
j = Next[j];
if(j == l2){
sum++;
j = Next[j];
}
}
printf("%d\n",sum);
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%s",word);
scanf("%s",text);
kmp();
}
return 0;
}