Cod sursa(job #253989)

Utilizator CezarMocanCezar Mocan CezarMocan Data 6 februarie 2009 13:59:46
Problema Episoade Scor 0
Compilator cpp Status done
Runda Stelele Informaticii 2009, clasele 9-10, ziua 1 Marime 3.17 kb
#include <cstdio>
#include <string>

using namespace std;

struct ct {
	int x, y;
};

int n, m, i, j, vf1, vf2, nr, ok;
char s[1010];
int v[110];
char st1[1010];
ct st2[1010], poz[1010];
char op;
ct c1, c2, cnou;



inline int max(int a, int b) {
	if (a > b)
		return a;
	return b;
}

inline int min(int a, int b) {
	if (a < b)
		return a;
	return b;
}

void baga1() {
	while (vf1 > 0 && st1[vf1] != '(') {
		c1 = st2[vf2];
		vf2--;
		c2 = st2[vf2];
		vf2--;
		if (st1[vf1] == '#') 
			if (c1.x == c2.y + 1 || c1.y == c2.x - 1) {
				cnou.x = min(c1.x, c2.x);
				cnou.y = max(c1.y, c2.y);
				vf2++;
				st2[vf2] = cnou;
			}
			else {
				ok = 0;
				return;
			}
		
		else 
			if (c1.x == c2.y + 1) {
				cnou.x = min(c1.x, c2.x);
				cnou.y = max(c1.y, c2.y);
				vf2++;
				st2[vf2] = cnou;
			}
			else {
				ok = 0;
				return;
			}
		vf1--;
	}
}


void baga2() {
	while (vf1 > 0 && st1[vf1] == '>' ) {
		c1 = st2[vf2];
		vf2--;
		c2 = st2[vf2];
		vf2--;
		if (st1[vf1] == '#') 
			if (c1.x == c2.y + 1 || c1.y == c2.x - 1) {
				cnou.x = min(c1.x, c2.x);
				cnou.y = max(c1.y, c2.y);
				vf2++;
				st2[vf2] = cnou;
			}
			else {
				ok = 0;
				return;
			}
		
		else 
			if (c1.x == c2.y + 1) {
				cnou.x = min(c1.x, c2.x);
				cnou.y = max(c1.y, c2.y);
				vf2++;
				st2[vf2] = cnou;
			}
			else {
				ok = 0;
				return;						
			}
		vf1--;
	}
}

void baga3() {
	while (vf1 > 0 && st1[vf1] != '(' ) {
		c1 = st2[vf2];
		vf2--;
		c2 = st2[vf2];
		vf2--;
		if (st1[vf1] == '#') 
			if (c1.x == c2.y + 1 || c1.y == c2.x - 1) {
				cnou.x = min(c1.x, c2.x);
				cnou.y = max(c1.y, c2.y);
				vf2++;
				st2[vf2] = cnou;
			}
			else {
				ok = 0;
				return ;
			}
		
		else 
			if (c1.x == c2.y + 1) {
				cnou.x = min(c1.x, c2.x);
				cnou.y = max(c1.y, c2.y);
				vf2++;
				st2[vf2] = cnou;
			}
			else {
				ok = 0;
				return;						
			}
		vf1--;
	}
}


int eval() {
	int i, j, nr;
	i = 0; ok = 1;
	vf1 = vf2 = 0;
	while (s[i] != 0) {
		if (s[i] == '#' || s[i] == '>') {
			if (s[i] == '#') {
				//evaluez pana dau de paranteza deschisa sau de baza stivei
				baga1();
				if (ok == 0)
					return 0;
				vf1++;
				st1[vf1] = '#';
			}
			if (s[i] == '>') {
				baga2();
				if (ok == 0)
					return 0;				
				vf1++;
				st1[vf1] = '>';
			}
		}
		
		if (s[i] >= '0' && s[i] <= '9') {
			nr = 0;
			while (s[i] >= '0' && s[i] <= '9' && s[i] != 0) {
				nr = nr * 10 + (s[i] - 48);
				i++;
			}
			i--;
			vf2++;
			st2[vf2] = poz[nr];
		}
		
		if (s[i] == '(') {
			vf1++;
			st1[vf1] = '(';
		}
		
		if (s[i] == ')') {
			baga3();
			if (ok == 0)
				return 0;			
			//evaluez pana la aia deschisa
		}
		i++;
	}
	
	return 1;
}

int main() {
	freopen("episoade.in", "r", stdin);
	freopen("episoade.out", "w", stdout);
	
	scanf("%s", s);
	
	for (i = 1; s[i] != 0; i++)
		s[i - 1] = s[i];
	s[0] = '(';
	s[strlen(s)] = ')';
	
	scanf("%d%d", &m, &n);
	
	for (int k = 1; k <= m; k++) {
		for (j = 1; j <= n; j++) {
			scanf("%d", &v[j]);
			poz[v[j]].x = poz[v[j]].y = j;
		}
	
		printf("%d\n", eval());
	}
	
	return 0;
}