Happy 2009
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2460 Accepted Submission(s): 824
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
给你一个字符串求其中‘happy’出现的个数(可跳跃字符)
这题要注意happy的顺序不能变,所以要最先产生h然后a然后p,p,y,给每个字母都对应一个数组的数,要产生a时必须有h,要产生p时必须有a,要产生happy时,必须有一个h,一个a,两个p,一个y;而且要保证所有的happy都没颠倒次序,所以每一个字母的个数不能超过前面那个字母的个数( 例如hayppahppy,这种只有一个 )。
开始一直不晓得,哎。。。
#include <iostream>
using namespace std;
const int Max=10010;
char s[Max];
int min(int a,int b)
{
return a<b?a:b;
}
int main()
{
while (gets(s))
{
int h=0,a=0,p=0,y=0;
for (int i=0;s[i]; i++)
{
if(s[i]=='h')
{
h++;
}
else if(s[i]=='a'&&a<h)
{
a++;
}
else if(s[i]=='p'&&p<2*a)
{
p++;
}
else if(s[i]=='y'&&y<(p/2))
{
y++;
}
}
int ans=min(min(h,a),min(p,y));
printf("%d\n",ans);
}
return 0;
}