hdu (2617) Happy 2009
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
Author
yifenfei
Problem analysis:
The problem want us count the number of "happy" in a string.Of couse, "happy" may not a word, eg: "hoa plpy", in the string it includes one "happy";
But you should notice one case:
For example: in the string "hy ppappa",it includes zero "happy",because it need a sequence of "h-a-p-p-y"; and the problem is mainly to solve this case;
At first,I have no ideas about this problem,and I have never solved this kind of problems before;
So here I post the code,and this represents a solution to some problems;
And the ideas come from others bolg;

1 #include<iostream> 2 #include<string> 3 using namespace std; 4 int main() 5 { 6 char str[10055]; 7 while(gets(str)) 8 { 9 int i; 10 int h,a,p1,p2,y; 11 h=a=p1=p2=y=0; 12 for(i=0;str[i]!='\0';i++) 13 { 14 if(str[i]=='h') 15 h++; 16 else if(str[i]=='a') 17 { 18 if(h>0) 19 { 20 h--;a++; 21 } 22 } 23 else if(str[i]=='p') 24 { 25 if(p1>0) 26 { 27 p1--;p2++; 28 } 29 else if(a>0) 30 { 31 a--;p1++; 32 } 33 } 34 else if(str[i]=='y') 35 { 36 if(p2>0) 37 { 38 p2--;y++; 39 } 40 } 41 42 }cout<<y<<endl; 43 44 } return 0; 45 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?