汉语题~
#include<iostream>
#include<vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <math.h>
#include<algorithm>
#define ll long long
#define eps 1e-8
using namespace std; int nexts[];
char str[],b[]; void pre_nexts(int m)
{
memset(nexts,,sizeof(nexts));
int j = ,k = -;
nexts[] = -;
while(j < m)
{
if(k == - || b[j] == b[k]) nexts[++j] = ++k;
else k = nexts[k];
}
}
int KMP(int n,int m)
{
int i,j = ,cnt = ;
for(i = ; i < n; i++)
{
while(j > && str[i] != b[j])
j = nexts[j];
if(str[i] == b[j])
{
j++;
}
if(j == m)cnt++,j++; //(出现个数)
//j = nexts[j];(出现次数) 注意区分
}
return cnt;
}
int main(void)
{
while(scanf("%s",str),str[] != '#')
{
scanf("%s",b);
if((int)strlen(b) > (int)strlen(str))
{
printf("0\n"); continue;
}
else
{
pre_nexts((int)strlen(b));
printf("%d\n",KMP((int)strlen(str),(int)strlen(b)));
}
}
return ;
}