| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
纯净水#include <iostream>
#include <string>
#include <stack>
#include <stdio.h>
using namespace std;
struct card{
char col, num;
};
int main() {
string s;
while(getline(cin, s)){
if(s[0] == '#') break;
stack<card> sc[53];
int prev[53] = {0}, suc[53] = {0};
for(int i = 0; i < 26; i++){
card temp;
temp.col = s[3*i], temp.num = s[3*i+1];
sc[i+1].push(temp);
}
getline(cin, s);
for(int i = 0; i < 26; i++){
card temp;
temp.col = s[3*i], temp.num = s[3*i+1];
sc[i+27].push(temp);
}
for(int i = 1; i <= 52; i++){
prev[i] = i-1;
suc[i] = (i+1)%53;
}
int ds = 52;
while(1){
bool kd = 0;
int cur = 1;
while(cur != 0){
int last3 = prev[prev[prev[cur]]];
if(last3 != 0 && (sc[last3].top().col == sc[cur].top().col || sc[last3].top().num == sc[cur].top().num)){
sc[last3].push(sc[cur].top());
sc[cur].pop();
if(sc[cur].empty()){
suc[prev[cur]] = suc[cur];
if(suc[cur] != 0) prev[suc[cur]] = prev[cur];
ds--;
}
kd = 1;
break;
}
int last1 = prev[cur];
if(last1 != 0 && (sc[last1].top().col == sc[cur].top().col || sc[last1].top().num == sc[cur].top().num)){
sc[last1].push(sc[cur].top());
sc[cur].pop();
if(sc[cur].empty()){
suc[prev[cur]] = suc[cur];
if(suc[cur] != 0) prev[suc[cur]] = prev[cur];
ds--;
}
kd = 1;
break;
}
cur = suc[cur];
}
if(!kd) break;
}
printf("%d piles remaining:", ds);
int cur = 1;
while(cur != 0){
printf(" %d", sc[cur].size());
cur = suc[cur];
}
printf("\n");
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator