HDU 2617 Happy 2009

本博客介绍了一个关于在给定字符串中拼凑出指定次数的'happy'子串的编程挑战。提供了输入和输出样例,以及实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接: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.
 

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相对次序不变。

#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值