首页 > ACM题库 > HDU-杭电 > hdu 2617 Happy 2009[解题报告]C++
2014
02-12

hdu 2617 Happy 2009[解题报告]C++

Happy 2009

问题描述 :

No matter you know me or not. Bless you happy in 2009.

输入:

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.

输出:

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.

样例输入:

hopppayppy happy
happ acm y
hahappyppy

样例输出:

2
1
2

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

这道题的解题关键是:存储,利用前一个字符是否符合要求,去判断:

#include <iostream>
using namespace std;
char sev[10010];
int main()
{
  int h,a,p,y;  		
  while(gets(sev))
  {
  	h=a=p=y=0;
  	int k(0);
   for(int i=0;sev[i];i++)
    {
      if(sev[i]=='h') h++;
      if(sev[i]=='a'){
  	 	  if(h)  {a++,h--; } 
	    }
	  
	  if(sev[i]=='p'){
  	     if(a){ k++; if(k%2==0){p++;a--;} }	
        }
	 
 	  if(sev[i]=='y'){
 		 if(p){
 		    y++;p--;	
 		  }
 	    } 			
    }
	
	cout<<y<<endl;	
  }	
	return 0;
}

解题转自:http://blog.csdn.net/youlingjisuan/article/details/8797261


  1. L(X [0 .. M-1],Y [0 .. N-1])= 1 + L(X [0 .. M-2],Y [0 .. N-1])这个地方也也有笔误
    应改为L(X [0 .. M-1],Y [0 .. N-1])= 1 + L(X [0 .. M-2],Y [0 .. N-2])