*2362 Chicken or the Egg http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2362 問題のルールを理解するのに少し時間のかかる問題。 卵鶏卵鶏鶏卵 と並んでいる場合並んでいる鶏で区切って (卵鶏卵鶏)と(鶏卵)で分割し、分割した内容を後ろから読んでより後ろがご先祖様になる。 この後ろから読むという部分を読解できるかどうかがこの問題最大の難関。 問題を解くコード自体は極めて簡素にできる。 #include<stdio.h> int main(){ char c,str[7],ans,old=' '; int count=0,ansCount; while(scanf("%c",&c)!=EOF && c!='\n'){ if(c=='e'){ scanf("%2s",str); }else if(c=='c'){ scanf("%6s",str); } if(c==old){ if(ansCount<count){ ans=c; ansCount=count; } count=0; }else if(old==' '){ ans=c; ansCount=1; count=0; } count++; old=c; } if(ansCount<count)ans=old; printf("%s\n",ans=='e'?"egg":"chicken"); }