Cod sursa(job #64913)

Utilizator scapryConstantin Berzan scapry Data 6 iunie 2007 11:07:59
Problema Perle Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.4 kb
#include <assert.h>
#include <stdio.h>

//this is nasty
#define printf(...) /*shut up*/;

enum { maxlen = 10002 };

int len;
int s[maxlen];
bool ans;
int end_pos;

bool match_A(int);
bool match_B(int);
bool match_C(int);

bool match_1(int pos)
{
	printf("match_1 pos %d\n", pos);

	if(pos >= len) return false;

	if(s[pos] == 1)
	{
		end_pos = pos + 1;
		return true;
	}
	else return false;
}

bool match_2(int pos)
{
	printf("match_2 pos %d\n", pos);

	if(pos >= len) return false;

	if(s[pos] == 2)
	{
		end_pos = pos + 1;
		return true;
	}
	else return false;
}

bool match_3(int pos)
{
	printf("match_3 pos %d\n", pos);

	if(pos >= len) return false;

	if(s[pos] == 3)
	{
		end_pos = pos + 1;
		return true;
	}
	else return false;
}

bool match_2B(int pos)
{
	printf("match_2B pos %d\n", pos);

	if(pos + 1 >= len) return false;

	if(s[pos] == 2) return match_B(pos + 1);
	else return false;
}

bool match_1A3AC(int pos)
{
	printf("match_1A3AC pos %d\n", pos);

	if(pos + 4 >= len) return false;

	if(s[pos] == 1 && s[pos + 2] == 3) return match_C(pos + 4);
	else return false;
}

bool match_3BC(int pos)
{
	printf("match_3BC pos %d\n", pos);

	if(pos + 3 >= len) return false;

	if(s[pos] == 3)
	{
		return match_B(pos + 1) && match_C(end_pos);
	}
	else return false;
}

bool match_12A(int pos)
{
	printf("match_12A pos %d\n", pos);

	if(pos + 2 >= len) return false;

	if(s[pos] == 1 && s[pos + 1] == 2) return match_A(pos + 2);
	else return false;
}

bool match_A(int pos)
{
	printf("match_A pos %d\n", pos);

	return match_1(pos) || match_2(pos) || match_3(pos);
}

bool match_B(int pos)
{
	printf("match_B pos %d\n", pos);

	return match_2B(pos) || match_1A3AC(pos);
}

bool match_C(int pos)
{
	printf("match_C pos %d\n", pos);

	return match_2(pos) || match_3BC(pos) || match_12A(pos);
}

void go()
{
	printf("string of length %d:\n", len);
	for(int i = 0; i < len; i++)
		printf("%2d   %d\n", i, s[i]);
	printf("\n");

	if( (match_A(0) && end_pos == len) ||
	    (match_B(0) && end_pos == len) ||
	    (match_C(0) && end_pos == len) ) ans = true;
	else ans = false;

	printf("ans %d\n\n", ans);
}

int main()
{
	int i, t, tests;
	FILE *f = fopen("perle.in", "r"),
	     *fo = fopen("perle.out", "w");
	if(!f || !fo) return 1;

	fscanf(f, "%d", &tests);
	for(t = 0; t < tests; t++)
	{
		fscanf(f, "%d", &len);
		for(i = 0; i < len; i++)
			fscanf(f, "%d", &s[i]);

		go();

		fprintf(fo, "%d\n", ans);
	}

	fclose(f);
	fclose(fo);
	return 0;
}