【字符串水题】HDU2617Happy 2009

来源:互联网 发布:canon mp230 清零软件 编辑:程序博客网 时间:2024/06/05 01:55

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2617

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 happyhapp acm yhahappyppy
 

Sample Output
212

看到我加粗放大的那句话没有,注意了,不要改变字母的位置!!所以我们需要加上判断,WA给跪了一次

代码:

#include<iostream>#include<cstdio>#include<string>#include<cstring>using namespace std;int main(){    string str;    while(getline(cin,str)){        int h=0,a=0,p=0,y=0;        for(int i=0;i<str.size();i++){            switch (str[i]){                case 'h':h++;break;                case 'a':if(h>a)a++;break;          //  加判断是为了保证不改变字母的位置;                case 'p':if(p<2*a)p++;break;                case 'y':if(p/2>y)y++;break;                default :break;            }        }        int t=min(min(h,a),min(y,p/2));        cout<<t<<endl;    }    return 0;}



0 0