The classic problem of flipping the cake, do directly: from the bottom to the top, the cake has been turned away, from the top down, a continuous turn over the good, the whole turn over.
/** Author:ben*/#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<ctime>#include<iostream>#include<algorithm>#include<queue>#include<Set>#include<map>#include<stack>#include<string>#include<vector>#include<deque>#include<list>#include<functional>#include<numeric>#include<cctype>using namespaceStd;typedefLong LongLL;Charpancakes[ the];BOOLpan[ the];intPannum;voidInline Flippan (intN) {BOOL*first =Pan; BOOL*last = First +N; while(First! = last) && (first! =--Last )) { BOOLTMP =! (*Last ); *last =! (*First ); *first =tmp; ++First ; } if(n%2==1) {Pan[n/2] =!pan[n/2]; }}intWork () {intAns =0; while(Pannum >0) { while(Pannum >0&& Pan[pannum-1]) {Pannum--; } if(Pannum <=0) { Break; } intI =0; while(Pan[i]) {I++; } if(I >0) {Flippan (I); Ans++; } flippan (Pannum); Ans++; } returnans;}intMain () {//freopen ("b.small.in", "R", stdin); intT; scanf ("%d", &T); for(intt =1; T <= t; t++) {scanf ("%s", pancakes); Pannum=strlen (pancakes); for(inti =0; i < Pannum; i++) { if(Pancakes[i] = ='-') {Pan[i]=0; } Else{Pan[i]=1; }} printf ("Case #%d:%d\n", T, work ()); } return 0;}
GCJ qualification Round, question B