Cod sursa(job #2376942)

Utilizator alex2kamebossPuscasu Alexandru alex2kameboss Data 8 martie 2019 19:21:35
Problema Perle Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.73 kb
#include <iostream>
#include <cstdio>
#include <stack>
#include <vector>

using namespace std;

int n,l, k, len;
int sir[10009];
stack <char> s;
int poz;

int verif(){
    while(!s.empty()){
        char x = s.top();
        s.pop();
        if(x=='B'){
            if(sir[poz]==2){
                s.push('B');
                ++poz;
                if(poz>=l-4)
                    return 0;
            }
            else if(sir[poz]==1 && sir[poz+2]==3){
                s.push('C');
                poz+=4;
                if(poz>l)
                    return 0;
            }
            else
                return 0;
        }
        else if(x=='C'){
            if(sir[poz]==2){
                poz++;
                if(poz>l && !s.empty())
                    return 0;
            }
            else if(sir[poz]==3){
                s.push('B');
                s.push('C');
                poz++;
                if(poz>=l-4)
                    return 0;
            }
            else if(sir[poz]==1 && sir[poz+1]==2){
                poz+=3;
                if(poz>l && !s.empty())
                    return 0;
            }
            else
                return 0;
        }
    }
    if(poz<=l)
        return 0;
    return 1;
}

int main()
{
    freopen("perle.in","r",stdin);
    freopen("perle.out","w",stdout);

    cin>>n;
    for(int i=0;i<n;++i){
        cin>>l;
        for(int j=1;j<=l;++j)
            cin>>sir[j];
        if(l==1){
            cout<<"1\n";
            continue;
        }
        s=stack<char>();
        s.push('B');
        poz=1;
        if(verif()){
            cout<<"1\n";
            continue;
        }
        s=stack<char>();
        s.push('C');
        poz=1;
        cout<<verif()<<"\n";
    }

    return 0;
}