题目链接:https://vjudge.net/problem/HDU-2087
题意:输入中含有一些数据,分别是成对出现的花布条和小饰条,输出能从花布条中剪出的最多小饰条个数。
Sample Input
abcde a3 aaaaaa aa #
Sample Output
0 3
代码如下:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char s1[1010],s2[1010];//母串,子串
int next[1010];
void getf(int L)//得到next数组
{
memset(next,0,sizeof(next));
int i=0,j=-1;
next[0]=-1;
while(i<L)
{
if(j<0||s2[i]==s2[j])
next[++i]=++j;
else
j=next[j];
}
}
int main()
{
while(~scanf("%s",s1))
{
if(strcmp(s1,"#")==0)
return 0;
scanf("%s",s2);
int L1=strlen(s1);
int L2=strlen(s2);
getf(L2);
int i=0,j=0;
int ans=0;
while(i<L1)
{
if(j<0||s1[i]==s2[j])
i++,j++;
else
j=next[j];
if(j==L2)
{
ans++;
j=0;//j=next[j];母串的字母可重复使用
}
}
printf("%d\n",ans);
}
return 0;
}