zoukankan      html  css  js  c++  java
  • HDU-2617

    Happy 2009

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 3276    Accepted Submission(s): 1072


    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”,当前字母可不可取是首要问题。

    当h的个数小于a时,a是可取的;

    同理,当a的个数小于p的一半时,p是可取的;

    当p的一半小于y时,y是可取的。

    最后所有符合条件的可取的y的个数就是“happy”的个数。

    注意:

    同一组数据中可能会存在空格,不要忽略了空格的读取。

    本文提供gets()和getline()两种读取整行的方法。

    AC代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 char s[10010];
     5 
     6 int main(){
     7     while(gets(s)){
     8         int len=strlen(s);
     9         int h=0,a=0,p=0,y=0;
    10         for(int i=0;i<len;i++){
    11             if(s[i]=='h')
    12             h++;
    13             else if(s[i]=='a'&&a<h)
    14             a++;
    15             else if(s[i]=='p'&&p/2<a)
    16             p++;
    17             else if(s[i]=='y'&&y<(p/2))
    18             y++;
    19         }
    20         cout<<y<<endl;
    21     }
    22     return 0;
    23 } 
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 
     4 string s;
     5 
     6 int main(){
     7     while(getline(cin,s)){
     8         int len=s.size();
     9         int h=0,a=0,p=0,y=0;
    10         for(int i=0;i<len;i++){
    11             if(s[i]=='h')
    12             h++;
    13             else if(s[i]=='a'&&a<h)
    14             a++;
    15             else if(s[i]=='p'&&p/2<a)
    16             p++;
    17             else if(s[i]=='y'&&y<(p/2))
    18             y++;
    19         }
    20         cout<<y<<endl;
    21     }
    22     return 0;
    23 } 
     
  • 相关阅读:
    Hibernate学习一----------Hibernate初实现
    Error executing DDL via JDBC Statement
    org.hibernate.MappingException:Unknown entity
    Struts2学习九----------处理结果类型(input)
    触发器-MySQL
    Struts2学习八----------接收参数
    Struts2学习七----------Struts2后缀
    Struts2学习六----------默认Action
    Struts2学习五----------指定多个配置文件
    Java 8 表示两个时间点距离
  • 原文地址:https://www.cnblogs.com/Kiven5197/p/6745265.html
Copyright © 2011-2022 走看看