COJ 0036 数数happy有多少个?

数数happy有多少个?
难度级别:B; 运行时间限制:1000ms; 运行空间限制:51200KB; 代码长度限制:2000000B
试题描述

    图图是个爱动脑子、观察能力很强的好学生。近期他正学英语单词,练字时无意识地写了一串小写英文字母,他发现这串字母中包含了很多个happy,他决定计算一下到底有多少个happy。规则是这样的:在该字符串提取任意位置的字符组成新的单词串,不改变其在原字符串中的相对顺序,请你编写程序帮助图图统计出新的单词串种最多有多少个happy。

输入
输入只有一行,含有一个字符串。字符串由小写字母a – z组成。此字符串长度不超过10000。
输出
一个非复整数。表示按照题目描述的规则最多能够组成单词happy的个数。
输入示例
hahappyppy
输出示例
2

题解:数据好水看我hack他们!我自认为我的写法挺好的了。。。(雾

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<cstring>
 7 #define PAU putchar(' ')
 8 #define ENT putchar('
')
 9 using namespace std;
10 inline int read(){
11     int x=0,sig=1;char ch=getchar();
12     while(!isdigit(ch)){if(ch=='-')sig=-1;ch=getchar();}
13     while(isdigit(ch))x=10*x+ch-'0',ch=getchar();
14     return x*=sig;
15 }
16 inline void write(int x){
17     if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;
18     int len=0,buf[15];while(x)buf[len++]=x%10,x/=10;
19     for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;
20 }
21 char ch;int cnt[4];
22 void init(){
23     for(ch=getchar();isalpha(ch);ch=getchar()){
24         if(ch=='h') cnt[0]++;
25         if(ch=='a') if(cnt[1]<cnt[0]) cnt[1]++;
26         if(ch=='p') if(cnt[2]<(cnt[1]<<1)) cnt[2]++;
27         if(ch=='y') if(cnt[3]<(cnt[2]>>1)) cnt[3]++;
28     }
29 }
30 void work(){
31     return;
32 }
33 void print(){
34     write(cnt[3]);
35     return;
36 }
37 int main(){init();work();print();return 0;}

原来愚蠢的code:

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<cstring>
 7 #define PAU putchar(' ')
 8 #define ENT putchar('
')
 9 using namespace std;
10 const int maxn=10000+10;
11 int s[4][maxn],tot[4],cnt[4];
12 int check(int d,int t){
13     for(;cnt[d]<tot[d];cnt[d]++){
14         if(s[d][cnt[d]]>t) return s[d][cnt[d]];
15     } return -1;
16 }
17 inline int read(){
18     int x=0,sig=1;char ch=getchar();
19     while(!isdigit(ch)){if(ch=='-')sig=-1;ch=getchar();}
20     while(isdigit(ch))x=10*x+ch-'0',ch=getchar();
21     return x*=sig;
22 }
23 inline void write(int x){
24     if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;
25     int len=0,buf[15];while(x)buf[len++]=x%10,x/=10;
26     for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;
27 }
28 char ch;
29 void init(){
30     int t=0;
31     for(ch=getchar();isalpha(ch);ch=getchar()){
32         if(ch=='h') s[0][tot[0]++]=t++;
33         else if(ch=='a') s[1][tot[1]++]=t++;
34         else if(ch=='p') s[2][tot[2]++]=t++;
35         else s[3][tot[3]++]=t++;
36     }
37     return;
38 }
39 int ans;
40 void work(){
41     for(int i=0;i<tot[0];i++){
42         int t=s[0][i];
43         if((t=check(1,t))<0)break;
44         if((t=check(2,t))<0)break;
45         if((t=check(2,t))<0)break;
46         if((t=check(3,t))<0)break;
47         ans++;
48     }
49     return;
50 }
51 void print(){
52     write(ans);
53     return;
54 }
55 int main(){init();work();print();return 0;}
原文地址:https://www.cnblogs.com/chxer/p/4591719.html