hdu2087 剪花布条

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

题目:

剪花布条

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19390    Accepted Submission(s): 12199


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

 

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

 

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

 

Sample Input
abcde a3 aaaaaa aa #
 

 

Sample Output
0 3
 

 

Author
qianneng
 

 

Source
 

 

Recommend
lcy
 思路:裸kmp
 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 #define MP make_pair
 6 #define PB push_back
 7 typedef long long LL;
 8 typedef pair<int,int> PII;
 9 const double eps=1e-8;
10 const double pi=acos(-1.0);
11 const int K=1e6+7;
12 const int mod=1e9+7;
13 
14 int nt[K];
15 char sa[K],sb[K];
16 
17 void kmp_next(char *T,int *nt)
18 {
19     nt[0]=0;
20     for(int i=1,j=0,len=strlen(T);i<len;i++)
21     {
22         while(j&&T[j]!=T[i])j=nt[j-1];
23         if(T[j]==T[i])j++;
24         nt[i]=j;
25     }
26 }
27 int kmp(char *S,char *T,int *nt)
28 {
29     int ans=0;
30     kmp_next(T,nt);
31     int ls=strlen(S),lt=strlen(T);
32     for(int i=0,j=0;i<ls;i++)
33     {
34         while(j&&S[i]!=T[j])j=nt[j-1];
35         if(S[i]==T[j])j++;
36         if(j==lt)
37              ans++,j=0;
38     }
39     return ans;
40 }
41 int main(void)
42 {
43     int t;
44     while(1)
45     {
46         scanf("%s",sa);
47         if(sa[0]=='#')break;
48         scanf("%s",sb);
49         printf("%d
",kmp(sa,sb,nt));
50     }
51     return 0;
52 }

 

原文地址:https://www.cnblogs.com/weeping/p/6669683.html