Cod sursa(job #351365)

Utilizator valentinroscaRosca Valentin valentinrosca Data 27 septembrie 2009 20:19:12
Problema Perle Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.17 kb
#include<fstream>
#include<stack>

using namespace std;

const char iname[]="perle.in";
const char oname[]="perle.out";

ifstream f(iname);
ofstream g(oname);

stack<int> S[2];

int i,j,n,k,v,valid[2];

int main()
{
    f>>n;
    for(;n;--n)
    {
        f>>k;
        if(k==1)
        {
            f>>v,g<<"1\n";
            continue;
        }

        S[0].push(5);
        S[1].push(6);
        valid[0]=valid[1]=1;
        for(i=1;i<=k;++i)
        {
            f>>v;
            for(j=0;j<2;++j)
                if(valid[j])
                {
                    if(S[j].empty())
                    {
                        valid[j]=0;
                        continue;
                    }
                    if(S[j].top()<4)
                    {
                        valid[j]=(S[j].top()==v);
                        S[j].pop();
                        continue;
                    }
                    if(S[j].top()==4)
                    {
                        S[j].pop();
                        continue;
                    }
                    if(S[j].top()==5)
                    {
                        if(v==3)
                        {
                            valid[j]=0;
                            S[j].pop();
                            continue;
                        }
                        else
                            if(v==1)
                                S[j].pop(),S[j].push(6),S[j].push(4),S[j].push(3),S[j].push(4);
                            else;

                        continue;
                    }

                    S[j].pop();
                    if(v==1)
                        S[j].push(4),S[j].push(2);
                    else
                        if(v==3)
                            S[j].push(6),S[j].push(5);
                }
        }
            if((valid[1]&&S[1].empty())||(valid[0]&&S[0].empty()))
                g<<"1\n";
            else
                g<<"0\n";
            while(!S[0].empty())
                S[0].pop();
            while(!S[1].empty())
                S[1].pop();
    }

    f.close();
    g.close();

    return 0;
}