Problem Description
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案。对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢?
Input
输入中含有一些数据,分别是成对出现的花布条和小饰条,其布条都是用可见ASCII字符表示的,可见的ASCII字符有多少个,布条的花纹也有多少种花样。花纹条和小饰条不会超过1000个字符长。如果遇见#字符,则不再进行工作。
Output
输出能从花纹布中剪出的最多小饰条个数,如果一块都没有,那就老老实实输出0,每个结果之间应换行。
Sample Input
abcde a3
aaaaaa aa
#
aaaaaa aa
#
Sample Output
0
3
3
#include
<iostream>
#include
<stdio.h>
#include
<algorithm>
#include
<cmath>
#include
<cstring>
//strstr()
using
namespace
std
;
int
main
(
)
{
char
str1
[
1001
]
,
str2
[
1001
]
;
while
(
cin
>>
str1
,
str1
[
0
]
!=
'#'
)
{
cin
>>
str2
;
char
*
p1
=
NULL
;
int
answer
=
0
;
char
*
p2
=
str1
;
while
(
(
p1
=
strstr
(
p2
,
str2
))
!=
NULL
)
{
answer
++
;
p2
=
p1
+
strlen
(
str2
)
;
}
cout
<<
answer
<<
endl
;
}
return
0
;
}
也可以用KMP来做,但是我还没学 (数据结构的内容)
strstr();
包含文件: cstring
功能:从字符串str1中查找是否有符串str2,如果有,从str1中的str2位置起,返回str1的指针,如果没有,返回null。返回值:返回该位置的 指针,如找不到,返回空指针。例子: char str[]="1234 xyz";char* str1=strstr(str,"34");cout<<str1<<endl;显示: 34 xyz