您的位置:首页 > 移动开发

COJ 0036 数数happy有多少个?

2015-06-21 10:59 323 查看
数数happy有多少个?
难度级别:B; 运行时间限制:1000ms; 运行空间限制:51200KB; 代码长度限制:2000000B
试题描述
图图是个爱动脑子、观察能力很强的好学生。近期他正学英语单词,练字时无意识地写了一串小写英文字母,他发现这串字母中包含了很多个happy,他决定计算一下到底有多少个happy。规则是这样的:在该字符串提取任意位置的字符组成新的单词串,不改变其在原字符串中的相对顺序,请你编写程序帮助图图统计出新的单词串种最多有多少个happy。

输入
输入只有一行,含有一个字符串。字符串由小写字母a – z组成。此字符串长度不超过10000。
输出
一个非复整数。表示按照题目描述的规则最多能够组成单词happy的个数。
输入示例
hahappyppy
输出示例
2
题解:数据好水看我hack他们!我自认为我的写法挺好的了。。。(雾

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#define PAU putchar(' ')
#define ENT putchar('\n')
using namespace std;
inline int read(){
int x=0,sig=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')sig=-1;ch=getchar();}
while(isdigit(ch))x=10*x+ch-'0',ch=getchar();
return x*=sig;
}
inline void write(int x){
if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;
int len=0,buf[15];while(x)buf[len++]=x%10,x/=10;
for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;
}
char ch;int cnt[4];
void init(){
for(ch=getchar();isalpha(ch);ch=getchar()){
if(ch=='h') cnt[0]++;
if(ch=='a') if(cnt[1]<cnt[0]) cnt[1]++;
if(ch=='p') if(cnt[2]<(cnt[1]<<1)) cnt[2]++;
if(ch=='y') if(cnt[3]<(cnt[2]>>1)) cnt[3]++;
}
}
void work(){
return;
}
void print(){
write(cnt[3]);
return;
}
int main(){init();work();print();return 0;}


原来愚蠢的code:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstring>
#define PAU putchar(' ')
#define ENT putchar('\n')
using namespace std;
const int maxn=10000+10;
int s[4][maxn],tot[4],cnt[4];
int check(int d,int t){
for(;cnt[d]<tot[d];cnt[d]++){
if(s[d][cnt[d]]>t) return s[d][cnt[d]];
} return -1;
}
inline int read(){
int x=0,sig=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')sig=-1;ch=getchar();}
while(isdigit(ch))x=10*x+ch-'0',ch=getchar();
return x*=sig;
}
inline void write(int x){
if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;
int len=0,buf[15];while(x)buf[len++]=x%10,x/=10;
for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;
}
char ch;
void init(){
int t=0;
for(ch=getchar();isalpha(ch);ch=getchar()){
if(ch=='h') s[0][tot[0]++]=t++;
else if(ch=='a') s[1][tot[1]++]=t++;
else if(ch=='p') s[2][tot[2]++]=t++;
else s[3][tot[3]++]=t++;
}
return;
}
int ans;
void work(){
for(int i=0;i<tot[0];i++){
int t=s[0][i];
if((t=check(1,t))<0)break;
if((t=check(2,t))<0)break;
if((t=check(2,t))<0)break;
if((t=check(3,t))<0)break;
ans++;
}
return;
}
void print(){
write(ans);
return;
}
int main(){init();work();print();return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: