Cod sursa(job #982109)

Utilizator mircea.dobreanuMircea Dobreanu mircea.dobreanu Data 8 august 2013 15:41:11
Problema Bool Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.76 kb
#include<cstdio>
#include<cstring>

const int MAXN=10010;
char s[MAXN],*p=0,aux[110];
int vf=-1,lgs,n;
int alfa[30];


void read();
bool eval();
bool termen();
bool neg();
bool value();
void solve();

int main()
{
	read();
	solve();
	return 0;
}

void read()
{
	int i;
	char c;
	freopen("bool.in","r",stdin);
	scanf("%c",&c);
	for (i=0; c!='\n'; ++i)
	{
		if (c>='A' && c<='Z')
			aux[++vf]=c;
		else if (c==' ' || c=='(' || c==')')
		{
			if (vf>=0)
				s[lgs++]=aux[vf--];
			if (c=='(' || c==')')
				s[lgs++]=c;
		}

		if (!strncmp(aux,"NOT",3) && vf>=2)
		{
			vf-=3;
			s[lgs++]='!';
		}
		else if (!strncmp(aux,"AND",3) && vf>=2)
		{
			vf-=3;
			s[lgs++]='&';
		}
		else if (!strncmp(aux,"OR",2) && vf>=1)
		{
			vf-=2;
			s[lgs++]='|';
		}
		else if (!strncmp(aux,"TRUE",4) && vf>=3)
		{
			vf-=4;
			s[lgs++]='1';
		}
		else if (!strncmp(aux,"FALSE",5) && vf>=4)
		{
			vf-=5;
			s[lgs++]='0';
		}
		scanf("%c",&c);
	}
	scanf("%d",&n);
	scanf("%s",aux);
	fclose(stdin);
}
bool eval()
{
	bool r=termen();
	while (*p=='|')
	{
		++p;
		r|=termen();
	}
	return r;
}
bool termen()
{
	bool r=neg();
	while (*p=='&')
	{
		++p;
		r&=neg();
	}
	return r;
}
bool neg()
{
	bool r=value();
	while (*p=='!')
	{
		++p;
		r=(!value());
	}
	return r;
}
bool value()
{
	bool r=false;
	if (*p=='(')
	{
		++p;
		r=eval();
		++p;
	}
	else
	{
		if (*p>='A' && *p<='Z')
		{
			r=alfa[*p-'A'+1];
			++p;
		}
		else if (*p>='0' && *p<='1')
		{
			r=*p-'0';
			++p;
		}
	}
	return r;
}
void solve()
{
	freopen("bool.out","w",stdout);
	int i;
	for (i=0; i<n; ++i)
	{
		p=s;
		alfa[aux[i]-'A'+1]=!alfa[aux[i]-'A'+1];
		printf("%d",eval());
	}
	printf("\n");
	fclose(stdout);
}