hdu (2617) Happy 2009
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.
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
Author
yifenfei
Problem analysis:
The problem want us count the number of "happy" in a string.Of couse, "happy" may not a word, eg: "hoa plpy", in the string it includes one "happy";
But you should notice one case:
For example: in the string "hy ppappa",it includes zero "happy",because it need a sequence of "h-a-p-p-y"; and the problem is mainly to solve this case;
At first,I have no ideas about this problem,and I have never solved this kind of problems before;
So here I post the code,and this represents a solution to some problems;
And the ideas come from others bolg;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<string> 3 using namespace std; 4 int main() 5 { 6 char str[10055]; 7 while(gets(str)) 8 { 9 int i; 10 int h,a,p1,p2,y; 11 h=a=p1=p2=y=0; 12 for(i=0;str[i]!='\0';i++) 13 { 14 if(str[i]=='h') 15 h++; 16 else if(str[i]=='a') 17 { 18 if(h>0) 19 { 20 h--;a++; 21 } 22 } 23 else if(str[i]=='p') 24 { 25 if(p1>0) 26 { 27 p1--;p2++; 28 } 29 else if(a>0) 30 { 31 a--;p1++; 32 } 33 } 34 else if(str[i]=='y') 35 { 36 if(p2>0) 37 { 38 p2--;y++; 39 } 40 } 41 42 }cout<<y<<endl; 43 44 } return 0; 45 }