標籤:
| 數數happy有多少個? |
| 難度層級:B; 已耗用時間限制:1000ms; Runspace限制: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(‘\n‘) 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(‘\n‘) 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;}
COJ 0036 數數happy有多少個?