一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案。对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢?
abcde a3 aaaaaa aa #
0 3
KMP的简单运用
题意:判断A字符串有多少个B
#include <iostream>
#include <cstdio>
#include <cstring>
char str1[1000],str2[1000];
int next[1000];
int main()
{
while(~scanf("%s",str1))
{
if(str1[0] == '#')
return 0;
scanf("%s",str2);
int l1 = strlen(str1);
int l2 = strlen(str2);
int i=0 , j = -1;
next[0] = -1;
while( i < l2)
{
if(j == -1 || str2[i] == str2[j])
{
i++;
j++;
next[i] = j;
}
else
{
j = next[j];
}
}
// for(int i = 0; i< l2; i++)
// printf("%d ",next[i]);
int p = 0 , q = 0;
int ans = 0;
while(p < l1)
{
if( q == -1 || str1[p] == str2[q])
{
p++;
q++;
if(q == l2)
{
ans++;
q = 0;
}
}
else
{
q = next[q];
}
}
printf("%d\n",ans);
}
return 0;
}