Cod sursa(job #329827)

Utilizator xaphariusMihai Suteu xapharius Data 7 iulie 2009 18:33:46
Problema Perle Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.63 kb
#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<vector>
using namespace std;

int main(){
	freopen("perle.in", "r", stdin);
	freopen("perle.out", "w", stdout);
	int N;
	vector<int> st;
	scanf("%d", &N);
	for (int i = 0, L; i < N; ++i){
		scanf("%d", &L);
		if (L == 1) {
			printf("1\n");
			continue;
		}
		int c;
		st.clear();
		scanf("%d", &c);
		switch (c){
			case 1:
				if (L == 3) {
					st.push_back('A');
					st.push_back(2);
				}
				else {
					st.push_back('C');
					st.push_back('A');
					st.push_back(3);
					st.push_back('A');
				}
				break;
			case 2:
				st.push_back('B');
				break;
			case 3:
				st.push_back('C');
				st.push_back('B');
				break;
		}
		bool ok;
		
		for (int j = 1; j < L; ++j){
			if (!st.size()){
				printf("0\n");
				break;
			}
			scanf("%d", &c);
			ok = true;
			switch (st[(int)st.size() - 1]){
				case 'A':
					st.pop_back();
					break;
				case 'B':
					if (c == 3) ok = false;
					else 
						if (c == 1) {
							st.pop_back();
							st.push_back('C');
							st.push_back('A');
							st.push_back(3);
							st.push_back('A');
						}
					break;
				case 'C':
					st.pop_back();
					if (c == 3) {
						st.push_back('C');
						st.push_back('B');
					}
					else 
						if (c == 1) {
							st.push_back('A');
							st.push_back(2);
						}
					break;
				default: 
					if (c == st[(int)st.size() - 1]) st.pop_back();
					else ok = false;
					break;
			}
			if (!ok) {
				printf("0\n");
				break;
			}
		}
		if (st.size()) printf("0\n");
		else if (ok) printf("1\n");
	}
	return 0;
}