Problem Description
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案。对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢?
Input
输入中含有一些数据,分别是成对出现的花布条和小饰条,其布条都是用可见ASCII字符表示的,可见的ASCII字符有多少个,布条的花纹也有多少种花样。花纹条和小饰条不会超过1000个字符长。如果遇见#字符,则不再进行工作。
Output
输出能从花纹布中剪出的最多小饰条个数,如果一块都没有,那就老老实实输出0,每个结果之间应换行。
Sample Input
abcde a3 aaaaaa aa #
Sample Output
0 3
//KMP算法实现;
# include<cstdio> # include<cstring> # include<string> # include<iostream> using namespace std; int main() { //freopen("a.txt","r",stdin); int i,j,sum,n[1200],l,len; char s[1200],t[1200]; while(scanf("%s",s)!=EOF&&s[0]!='#') { scanf("%s",t); l=strlen(s); len=strlen(t); i=0;j=-1; n[0]=-1; while(i<len) { if(j==-1||t[i]==t[j]) { i++;j++; if(t[i]!=t[j]) n[i]=j; else n[i]=n[j]; } else j=n[j]; } i=j=0; sum=0; while(i<l) { if(j==-1||s[i]==t[j]) { i++;j++; } else j=n[j]; if(j>=len) { sum++;j=0; } } printf("%d\n",sum); } return 0; }