Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

纯净水

Posted by KatrineYang at 2016-08-27 05:18:13 on Problem 1214
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator