Cod sursa(job #107677)

Utilizator alex3el_n2oAlex Vladescu alex3el_n2o Data 20 noiembrie 2007 10:17:20
Problema Perle Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.67 kb
#include <stdio.h>
#include <string.h>
FILE *f=fopen("perle.in","r"),*g=fopen("perle.out","w");
int k[15002],lg;
char s[20002];
int sol(char tip,int tip2,int poz)
{
	char aux1[100],aux2[100];
	aux1[0]=NULL;
	aux2[0]=NULL;
	strcpy(aux1,s);
	aux1[poz]=NULL;
	if (s[0]!=NULL) strcpy(aux2,s+poz+1);
	if (tip=='A')
		{
		if (tip2==1) strcat(aux1,"1");
		if (tip2==2) strcat(aux1,"2");
		if (tip2==3) strcat(aux1,"3");
		}
	if (tip=='B')
		{
		if (tip2==1) strcat(aux1,"2B");
		if (tip2==2) strcat(aux1,"1A3AC");
		}
	if (tip=='C')
		{
		if (tip2==1) strcat(aux1,"2");
		if (tip2==2) strcat(aux1,"3BC");
		if (tip2==3) strcat(aux1,"12A");
		}
	strcat(aux1,aux2);
	strcpy(s,aux1);
	lg=strlen(s);
	return 0;
}
int main()
{
int n,l,i,j,p;
fscanf(f,"%d",&n);
for (i=1;i<=n;i++)
	{
	fscanf(f,"%d",&l);
	for (j=1;j<=l;j++)
		fscanf(f,"%d",&k[j]);
	p=1;
	if (l==1)
		fprintf(g,"1\n");
	else
	while ((p)&&(lg<=l))
		{
		if (lg==0)
			{
			if (k[1]==1)
				{
				sol('B',2,1);
				sol('C',3,1);
				}
			if (k[1]==2)
				{
				sol('B',1,1);
				}
			if (k[1]==3)
				{
				sol('C',2,1);
				}
			}
		else
			{
			p=0;
			j=0;
			while ((j<lg)&&(!p))
				{
				if (s[j]=='A')
					{
					sol('A',k[j+1],j);
					p=1;
					}
				if ((s[j]=='B')&&(!p))
					{
					if (k[j+1]==2) sol('B',1,j);
					if (k[j+1]==1) sol('B',2,j);
					p=1;
					}
				if ((s[j]=='C')&&(!p))
					{
					if (k[j+1]==2) sol('C',1,j);
					if (k[j+1]==3) sol('C',2,j);
					if (k[j+1]==1) sol('C',3,j);
					p=1;
					}
				j++;
				}
			}
		}
	if ((lg==l)&&(l>1)) fprintf(g,"1\n");
	if ((lg!=l)&&(l>1)) fprintf(g,"0\n");
	s[0]=NULL;
	lg=0;
	}
return 0;
}