Cod sursa(job #1140316)

Utilizator mihai995mihai995 mihai995 Data 11 martie 2014 21:48:27
Problema Perle Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.88 kb
#include <fstream>
using namespace std;

const int N = 1 + 1e4, inf = 0x3f3f3f3f;

int v[N], n;

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

int matchB(int), matchC(int);

int matchB(int p){
    if (p > n)
        return inf;
    if ( v[p] == 1 && v[p + 2] == 3)
        return matchC(p + 4);
    if ( v[p] == 2 )
        return matchB(p + 1);
    return inf;
}

int matchC(int p){
    if ( v[p] == 1 && v[p + 1] == 2)
        return p + 2;
    if ( v[p] == 2 )
        return p;
    if ( v[p] == 3 )
        return matchC( 1 + matchB(p + 1) );
    return inf;
}

bool compute(){
    for (int i = 0 ; i <= v[0] ; i++)
        in >> v[i];
    if (v[0] == 1)
        return true;
    return matchB(1) == v[0] || matchC(1) == v[0];
}

int main(){
    int times;

    in >> times;
    while (times--)
        out << compute() << "\n";

    return 0;
}