Cod sursa(job #873178)

Utilizator Kira96Denis Mita Kira96 Data 6 februarie 2013 22:19:53
Problema Bool Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.17 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=0;i<=n;++i)
	{
		if(i==n)
		{
			i=i;
			i++;
			i--;
		}
		if(s[i]==' ')
			continue;
		if(s[i]=='(')
		{
			st.push(s[i]);
			nr['(']++;
		}
		else
			if((s[i]<='Z'&&s[i]>='A')&&(s[i+1]==' '||s[i+1]==')'))
			{
				ex[++t]=s[i];
			}
			else
			{
				if(s[i]=='O')
				{
					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]=='A')
				{
					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]=='N')
				{
					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]=='T')
				{
					c='t';
					ex[++t]=c;
					i+=3;
				}
				else
				if(s[i]=='F')
				{
					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();
					nr['(']--;
				}
			}
	}
	while(!st.empty()&&st.top()!='('&&gr[st.top()]>=gr[c])
	{
		ex[++t]=st.top();
		st.pop();
	}
	scanf("%d",&m);
	scanf("%s",s);
	n=strlen(s);
	for(i=0;i<n;++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;
}