HDU-2617

Happy 2009

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3276    Accepted Submission(s): 1072


Problem Description
No matter you know me or not. Bless you happy in 2009.
 

 

Input
The input contains multiple test cases.
Each test case included one string. There are made up of ‘a’-‘z’ or blank. The length of string will not large than 10000. 
 

 

Output
For each test case tell me how many times “happy” can be constructed by using the string. Forbid to change the position of the characters in the string. The answer will small than 1000.
 

 

Sample Input
hopppayppy happy
happ acm y
hahappyppy
 

 

Sample Output
2
1
2
 

 

 

对于此题,我们先对目标字符串“happy”分析,发现要想组成“happy”,当前字母可不可取是首要问题。

当h的个数小于a时,a是可取的;

同理,当a的个数小于p的一半时,p是可取的;

当p的一半小于y时,y是可取的。

最后所有符合条件的可取的y的个数就是“happy”的个数。

 

注意:

同一组数据中可能会存在空格,不要忽略了空格的读取。

本文提供gets()和getline()两种读取整行的方法。

 

AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 char s[10010];
 5 
 6 int main(){
 7     while(gets(s)){
 8         int len=strlen(s);
 9         int h=0,a=0,p=0,y=0;
10         for(int i=0;i<len;i++){
11             if(s[i]=='h')
12             h++;
13             else if(s[i]=='a'&&a<h)
14             a++;
15             else if(s[i]=='p'&&p/2<a)
16             p++;
17             else if(s[i]=='y'&&y<(p/2))
18             y++;
19         }
20         cout<<y<<endl;
21     }
22     return 0;
23 } 

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 string s;
 5 
 6 int main(){
 7     while(getline(cin,s)){
 8         int len=s.size();
 9         int h=0,a=0,p=0,y=0;
10         for(int i=0;i<len;i++){
11             if(s[i]=='h')
12             h++;
13             else if(s[i]=='a'&&a<h)
14             a++;
15             else if(s[i]=='p'&&p/2<a)
16             p++;
17             else if(s[i]=='y'&&y<(p/2))
18             y++;
19         }
20         cout<<y<<endl;
21     }
22     return 0;
23 } 

 

 
posted @ 2017-04-21 19:58  Kiven#5197  阅读(208)  评论(0编辑  收藏  举报