標籤:c++
Problem Description一塊花布條,裡面有些圖案,另有一塊直接可用的小飾條,裡面也有一些圖案。對於給定的花布條和小飾條,計算一下能從花布條中儘可能剪出幾塊小飾條來呢?
Input輸入中含有一些資料,分別是成對出現的花布條和小飾條,其布條都是用可見ASCII字元表示的,可見的ASCII字元有多少個,布條的花紋也有多少種花樣。花紋條和小飾條不會超過1000個字元長。如果遇見#字元,則不再進行工作。
Output輸出能從花紋布中剪出的最多小飾條個數,如果一塊都沒有,那就老老實實輸出0,每個結果之間應換行。
Sample Input
abcde a3aaaaaa aa#
Sample Output
03
#include<iostream>#include<cstdio>#include<cstring>using namespace std;char s1[1005],s2[1005];int next[1005];void getnext(char *s2){int i=0,j=-1;next[0]=-1;while(i<strlen(s2)){if(j==-1||s2[i]==s2[j]){i++;j++;if(s2[i]!=s2[j]) next[i]=j;else next[i]=next[j];}elsej=next[j];}}int main(){int s,t1,t2;while(scanf("%s",s1)){s=0;if(s1[0]=='#'&&s1[1]=='\0')break;scanf("%s",s2);t1=strlen(s1);t2=strlen(s2);getnext(s2);if(t2>t1){cout<<0<<endl;continue;}int i=0,j=0;while(i<t1){if(j==-1||s1[i]==s2[j]){i++;j++;}elsej=next[j];if(j==t2){j=0;s++;}}cout<<s<<endl;}return 0;}
作為模板吧。。
HDU 2087 剪花布條 KMP入門