您的位置:首页 > 其它

HDOJ 2087 剪花布条

2015-09-10 16:49 134 查看


剪花布条

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 11669    Accepted Submission(s): 7502


Problem Description

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

 

Input

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

 

Output

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

 

Sample Input

abcde a3
aaaaaa aa
#

 

Sample Output

0
3

 

Author

qianneng

 

Source

冬练三九之二

 

Recommend

lcy   |   We have carefully selected several similar problems for you:  3746 3336 1358 2091 2094 

 

#include <cstdio>
#include <cstring>

const int N = 1010;
char A
, B
;
int next
;

void get_next() {
int i = 0, j = -1;
next[i] = j;

while (B[i]) {
if (j == -1 || B[i] == B[j]) {
++i, ++j;
if (B[i] != B[j])
next[i] = j;
else
next[i] = next[j];
}
else
j = next[j];
}
}

int KMP() {
int cnt = 0;
int i = 0, j = 0;

get_next();

while (A[i]) {
if (A[i] == B[j])
++i, ++j;
else {
if (next[j] == -1)
++i, j = 0;
else
j = next[j];
}
if (!B[j]) {
j = 0;
cnt++;
}
}

return cnt;
}

int main() {

while (~scanf("%s%s", A, B), A[0] != '#')
printf("%d\n", KMP());

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: