题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2617
Happy 2009
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2543 Accepted Submission(s): 853
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
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int main()
{
string s;
int a[5];
while(getline(cin,s))
{
int sum=0;
memset(a,0,sizeof(a));
for(int i=0;i<s.size();i++)
{
if(s[i]=='h') a[0]++;
else if(s[i]=='a'&&a[0]&&a[1]<a[0]) a[1]++;
else if(s[i]=='p'&&a[0]&&a[1]&&a[2]<a[1]*2) a[2]++;
else if(s[i]=='y'&&a[0]&&a[1]&&a[2]>1)
{
a[0]--;a[1]--;a[2]-=2;
sum++;
}
}
cout<<sum<<endl;
}
return 0;
}