LeeBlog

导航

HDU 2617 Happy 2009

这题深受北大那题影响啊,这是链接http://www.cnblogs.com/Lvsi/archive/2011/04/10/2011484.html

这题要注意happy的顺序不能变,所以要最先产生h然后a然后p,p,y,给每个字母都对应一个数组的数,要产生a时必须有h,要产生p时必须有a,要产生happy时,必须有一个h,一个a,两个p,一个y;而且要保证所有的happy都没颠倒次序,所以每一个字母的个数不能超过前面那个字母的个数( 例如hayppahppy,这种只有一个 )

#include<stdio.h>
#include<string.h>
int n,num[5];
char str2[100005];//happy
void cal(  )
{
     int k = 0;
     while( str2[k] )
     {
            if( str2[k] == 'h' )
                ++num[0];
            if( str2[k] == 'a' && num[0] && num[1] < num[0]  )
                num[1]++;
            if( str2[k] == 'p' && num[1] && ( num[2] < 2 * num[1] ) )
                num[2]++;
            if( str2[k] == 'y' && num[2] >= 2 && num[1] && num[0] )
            {
                ++n;
                num[0] -= 1;
                num[1] -= 1;
                num[2] -= 2;
            }
            ++k;
            }
 }
int main( )
{
    while( gets( str2 ) )
    {
           memset( num,0,sizeof( num ) );
           n = 0;
           cal( );
           printf( "%d\n",n );
           }
    return 0;
}

posted on 2011-05-06 15:32  LeeBlog  阅读(258)  评论(0编辑  收藏  举报