Cod sursa(job #873208)

Utilizator Kira96Denis Mita Kira96 Data 6 februarie 2013 22:41:23
Problema Bool Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.09 kb
#include<stdio.h>
#include<stack>
#include<string.h>
using namespace std;
int k,to,i,n,gr[300],t,c,m,nr[2000];
bool fk[2000],v[400];
char s[2000],ex[2000];
stack <char> st;
int main ()
{
	freopen ("bool.in","r",stdin);
	freopen ("bool.out","w",stdout);
	v['t']=1;
	gr['o']=0;
	gr['a']=1;
	gr['n']=2;
	gets(s+1);
	s[0]='(';
	n=strlen(s);
	s[n]=')';
	for(i=n;i>=0;--i)
	{
		if(s[i]==' ')
			continue;
		if(s[i]==')')
		{
			st.push(s[i]);
		}
		else
			if((s[i]<='Z'&&s[i]>='A')&&(s[i-1]==' '||s[i-1]=='('))
			{
				ex[++t]=s[i];
			}
			else
			{
				if(s[i]=='R')
				{
					c='o';
					while(!st.empty()&&st.top()!=')'&&gr[st.top()]>=gr[c])
					{
						ex[++t]=st.top();
						st.pop();
					}
					st.push(c);
					i--;
				}
				else
				if(s[i]=='D')
				{
					c='a';
					while(!st.empty()&&st.top()!=')'&&gr[st.top()]>=gr[c])
					{
						ex[++t]=st.top();
						st.pop();
					}
					st.push(c);
					i-=2;
				}
				else
				if(s[i]=='T')
				{
					c='n';
					while(!st.empty()&&st.top()!=')'&&gr[st.top()]>gr[c])
					{
						ex[++t]=st.top();
						st.pop();
					}
					st.push(c);
					i-=2;
				}
				else
				if(s[i]=='E'&&s[i-1]=='U')
				{
					c='t';
					ex[++t]=c;
					i-=3;
				}
				else
				if(s[i]=='E')
				{
					c='f';
					ex[++t]=c;
					i-=4;
				}
				else
				{
					while(!st.empty()&&st.top()!=')'&&gr[st.top()]>=gr[c])
					{
						ex[++t]=st.top();
						st.pop();
					}
					if(st.top()==')')
					st.pop();
				}
			}
	}
	while(!st.empty()&&st.top()!='('&&gr[st.top()]>=gr[c])
	{
		ex[++t]=st.top();
		st.pop();
	}
	scanf("%d\n",&m);
	scanf("%s",s);
	for(i=0;i<m;++i)
	{
		v[s[i]]=!v[s[i]];
		for(k=1;k<=t;++k)
		{
			if((ex[k]>='A'&&ex[k]<='Z')||(ex[k]=='t'||ex[k]=='f'))
			{
				fk[++to]=v[ex[k]];
			}
			else
			{
				switch(ex[k])
				{
				case 'o': fk[to-1]=fk[to]||fk[to-1]; break;
				case 'a': fk[to-1]=fk[to]&&fk[to-1]; break;
				case 'n': fk[to]=!fk[to]; break;
				}
				if(ex[k]!='n')
					--to;
			}
		}
		printf("%d",fk[1]);
		--to;
	}
	return 0;
}