1. 程式人生 > >【字串水題】HDU2617Happy 2009

【字串水題】HDU2617Happy 2009

題目連結: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 happy happ acm y hahappyppy
Sample Output 2 1 2

看到我加粗放大的那句話沒有,注意了,不要改變字母的位置!!所以我們需要加上判斷,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;
}