题意:求w串在T串中的出现次数。(不可重叠出现)
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 1000 + 10;
const int MAXT = 1000000 + 10;
using namespace std;
char s1[MAXN], s2[MAXN];
int len1, len2;
int Next[MAXN];
void getNext(){
Next[0] = 0;
for(int p = 1, k = 0; p < len2; ++p){
while(k > 0 && s2[p] != s2[k]) k = Next[k - 1];
if(s2[p] == s2[k]) ++k;
Next[p] = k;
}
}
int kmp(){
getNext();
int cnt = 0;
for(int p = 0, k = 0; p < len1; ++p){
while(k > 0 && s1[p] != s2[k]) k = Next[k - 1];
if(s1[p] == s2[k]) ++k;
if(k == len2){
++cnt;
k = 0;
}
}
return cnt;
}
int main(){
while(scanf("%s", s1) == 1){
if(strcmp(s1, "#") == 0) return 0;
scanf("%s", s2);
len1 = strlen(s1);
len2 = strlen(s2);
printf("%d\n", kmp());
}
return 0;
}