hdu 2087:剪花布条

http://acm.hdu.edu.cn/showproblem.php?pid=2087

Problem Description

一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案。对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢?

 

 

Input

输入中含有一些数据,分别是成对出现的花布条和小饰条,其布条都是用可见ASCII字符表示的,可见的ASCII字符有多少个,布条的花纹也有多少种花样。花纹条和小饰条不会超过1000个字符长。如果遇见#字符,则不再进行工作。

 

 

Output

输出能从花纹布中剪出的最多小饰条个数,如果一块都没有,那就老老实实输出0,每个结果之间应换行。

 

 

Sample Input


 

abcde

a3

aaaaaa

aa

#

 

Sample Output

0

3

题意找出a字符串中最多可以找出b字符串,但是剪过之后就不能再剪了。一道类似的题https://blog.csdn.net/qq_41505957/article/details/81712690

 

#include<stdio.h>
#include<string.h>
#define N 1020
int next[N],l1,l2,ans;
char str1[N],str2[N];
void get_next()
{
	int i,j;
	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];
	}
}
void kmp()
{
	get_next();
	int i,j;
	i=0;
	j=0;
	while(i<l1)
	{
		if(j==-1||str1[i]==str2[j])
		{
			i++;
			j++;
		}
		else
		j=next[j];
		if(j==l2)
		{
			j=0;
			ans++;
		}
	}
}
int main()
{
	while(scanf("%s",str1)!=EOF)
	{
		ans=0;
		l1=strlen(str1);
		if(str1[0]=='#'&&l1==1)
			break;
		scanf("%s",str2);
		l2=strlen(str2);
		memset(next,0,sizeof(next));
		kmp();
		printf("%d\n",ans);
	}
	return 0;
}

 

posted @ 2018-08-16 00:07  宿星  阅读(114)  评论(0编辑  收藏  举报