B - 再来一道
Problem Description
XiaoMing is so lazy that he was sleeping in his English lessons, so his English teacher punished him to calculate how many times
that "sleep" and "sleepy" appear altogether in a given English string, and the string’s length is not greater than 1,000,000.
Input
The first line of the input contains an integer T(1<=T<=11) which means the number of test cases.
Each case contains a English string(which maybe contains lowercase, ‘,’, ’.’, and ’?’, but not ‘ (space)‘) in a single line.
Output
For each case, you need to print the answer in a single line.
Sample Input
5 sleep.....y sl.eep xxsleepsleepyyyboring sleeppppppppppppppppppp fifjfjo,hhhfijdfij.efejo?fdffdf
Sample Output
1 0 3 1 0
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
27
28
29
30
31
32
33
34
35
36
37
38
39
|
/*
* this code is made by 1406100108
* Problem: 1353
* Verdict: Accepted
* Submission Date: 2015-04-14 14:29:13
* Time: 12 MS
* Memory: 2380 KB
*/
#include<iostream>
#include<cstring>
using
namespace
std;
int
main()
{
char
text[1000001];
unsigned
int
i,lg,T ,count; //lg为字符串长
cin>>T;
while
(T--)
{
count = 0;
cin>>text;
lg=
strlen
(text);
for
(i=0;i<lg;++i)
{
if
(text[i]==
's'
)
if
(text[i+1]==
'l'
)
if
(text[i+2]==
'e'
)
if
(text[i+3]==
'e'
)
if
(text[i+4]==
'p'
)
{
++count;
if
(text[i+5]==
'y'
)
++count;
}
}
cout<<count<<endl;
}
return
0;
}
|
这道题比较简单,主要就是寻找sleep和sleepy,注意在寻找到sleep时再后面可以再判断一下有没有y,有则累加。用这种方法破解有点暴力,缺乏简洁,这里还可用<string>
库里的字符串寻找函数,这样的话定义时应该定义成string str。用直接寻找串内的子串。