Problem Description
Problem Analysis
Since comparison can stop as soon as a mismatch is found,
it is more convenient to write this as a function.
Note the second output group:
it shows that substrings already “cut out” need to be skipped.
So the increment of the loop variable i is handled conditionally.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| #include<stdio.h>
#include<string.h>
char s1[1020],s2[1020];
bool judge(int b,int l2) {
for(int i=0; i<l2; i++) {
if(s1[b+i]!=s2[i])
return false;
}
return true;
}
int main() {
while(scanf("%s",s1),s1[0]!='#') {
scanf("%s",s2);
int l1=strlen(s1),l2=strlen(s2);
int cnt=0;
for(int i=0; i<l1;) {
if(judge(i,l2)) {
i+=l2;
cnt++;
} else
i++;
}
printf("%d\n",cnt);
}
return 0;
}
|
Problem link: HDU 2087 Cut the Flower Cloth Strip