// @BEGIN_OF_SOURCE_CODE /* @JUDGE_ID: 17243NT 127 C++ "" */ #include #include #ifdef ONLINE_JUDGE #define ins cin #define outs cout #else #define ins fin #define outs fout ifstream fin("myprog.in"); ofstream fout("myprog.out"); #endif typedef struct { char s; char r; } card; typedef struct { int n; card c[52]; } pile; int npile; pile piles[52]; void newpile(char r, char s) { piles[npile].n = 1; piles[npile].c[0].r = r; piles[npile].c[0].s = s; npile++; } void movepile(int n, int s) { piles[n - s].c[piles[n - s].n++] = piles[n].c[--piles[n].n]; if(piles[n].n <= 0) { for(int i = n; i < npile - 1; i++) piles[i] = piles[i + 1]; npile--; } } int checkpile(int n) { if(n < 1) return 0; if(n < 3) { if(piles[n - 1].c[piles[n - 1].n - 1].r == piles[n].c[piles[n].n - 1].r) return 1; if(piles[n - 1].c[piles[n - 1].n - 1].s == piles[n].c[piles[n].n - 1].s) return 1; return 0; } if(piles[n - 3].c[piles[n - 3].n - 1].r == piles[n].c[piles[n].n - 1].r) return 3; if(piles[n - 3].c[piles[n - 3].n - 1].s == piles[n].c[piles[n].n - 1].s) return 3; if(piles[n - 1].c[piles[n - 1].n - 1].r == piles[n].c[piles[n].n - 1].r) return 1; if(piles[n - 1].c[piles[n - 1].n - 1].s == piles[n].c[piles[n].n - 1].s) return 1; return 0; } int main() { int i, j, t; char suit, rank; for(;;) { ins >> rank; if(rank == '#') break; ins.putback(rank); for(i = 0; i < 52; i++) { ins >> rank >> suit; newpile(rank, suit); for(;;) { for(j = 0; j < npile; j++) { t = checkpile(j); if(t != 0) break; } if(t == 0) break; movepile(j, t); } } if(npile > 1) outs << npile << " piles remaining:"; else outs << "1 pile remaining:"; for(i = 0; i < npile; i++) outs << " " << piles[i].n; outs << endl; npile = 0; } return 0; } // @END_OF_SOURCE_CODE