Cod sursa(job #3328200)

Utilizator Andrei_PanaAndrei Pana Andrei_Pana Data 6 decembrie 2025 20:03:02
Problema Perle Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <fstream>
using namespace std;

ifstream cin("perle.in");
ofstream cout("perle.out");

#define MAXN 10000

int v[MAXN];

int n,pos;
int check(char type){
  if(pos==n){
    return 0;
  }

  if(type=='A'){
    pos++;
    return 1;
  }else if(type=='B'){
    //B->2B
    if(v[pos]==2){
      pos++;
      return check('B');
    }

    //B->1A3AC
    if(v[pos]==1){
      pos++;
      if(!check('A')){
        return 0;
      }
      if(pos==n||v[pos]!=3){
        return 0;
      }
      pos++;

      return (check('A')&&check('C'));
    }
  }else{
    //C->2
    if(v[pos]==2){
      pos++;
      return 1;
    }

    //C->12A
    if(v[pos]==1){
      pos++;
      if(pos==n||v[pos]!=2){
        return 0;
      }
      pos++;
      return check('A');
    }

    //C->3BC
    if(v[pos]==3){
      pos++;
      return (check('B')&&check('C'));
    }
  }

  return 0;
}

int main(){
  int t,i;

  cin>>t;
  while(t--){
    cin>>n;
    for(i=0;i<n;i++){
      cin>>v[i];
    }
    pos=0;
    if(check('A')&&pos==n){
      cout<<1<<"\n";
      continue;
    }

    pos=0;
    if(check('B')&&pos==n){
      cout<<1<<"\n";
      continue;
    }

    pos=0;
    if(check('C')&&pos==n){
      cout<<1<<"\n";
      continue;
    }

    cout<<0<<"\n";
  }

  return 0;
}