Cod sursa(job #536334)

Utilizator tudorsTudor Siminic tudors Data 18 februarie 2011 16:03:53
Problema Perle Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <fstream>
using namespace std;
int n,t,z,i,L[10001	],poz;
char ch;

int a();
int b();
int c();
char pornire();

ifstream f("perle.in");
ofstream g("perle.out");

int main()
{
	f>>t;
	for (z=1;z<=t;++z)
	{
		f>>n;
		for (i=1;i<=n;++i)
			f>>L[i];
		ch=pornire();
		poz=1;
		switch(ch)
		{
		case 'A': g<<a()<<endl; break;
		case 'B': g<<b()<<endl; break;
		case 'C': g<<c()<<endl; break;
		}
	}
	f.close();
	g.close();
	return 0;
}

int a()
{
	if (poz>n)
		return 0;
	poz++;
	return 1;
}

int b()
{
	int t;
	if (poz>n)
		return 0;
	if (L[poz]==2)
	{
		poz++;
		return b();
	}
	if (L[poz]==1)
	{
		poz++;
		t=a();
		if (t==0)
			return 0;
		if (poz>n)
			return 0;
		if (L[poz]!=3)
			return 0;
		poz++;
		t=a();
		if (t==0)
			return 0;
		return c();
	}
	return 0;
}

int c()
{
	int t;
	if (poz>n)
		return 0;
	if (L[poz]==2)
	{
		poz++;
		return 1;
	}
	if (L[poz]==3)
	{
		poz++;
		t=b();
		if (t==0)
			return 0;
		return c();
	}
	if (L[poz]==1)
	{
		poz++;
		if ((poz>n) || (L[poz]!=2))
			return 0;
		poz++;
		return a();
	}
	return 0;
}

char pornire()
{
	if (n==1)
		return 'A';
	if ((L[1]==1) && (L[2]==2) && (n==3))
		return 'C';
	if (L[1]==3)
		return 'C';
	if (L[1]==2)
		return 'B';
	if (L[1]==1)
		return 'B';
}