Cod sursa(job #845055)

Utilizator stoicatheoFlirk Navok stoicatheo Data 30 decembrie 2012 13:33:12
Problema Episoade Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.76 kb
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
 
using namespace std;
 
#define Pair pair<int,int>
 
#define F first
#define S second
 
#define pb push_back
#define Mp make_pair
 
#define maxn 1005
 
char s[maxn];
 
int n,t,i,j,p;
int Ps[maxn],ok,poz=0;
 
bool operator<(const Pair &a,const Pair &b)
{ return a.F<b.F; }
 
Pair eval(int nivel)
{
    if (nivel==0)
    {
        Pair r;
         
        vector<Pair > V;
         
        for ( V.pb( eval(nivel+1) ); s[poz]=='#' ;)
        {   ++poz;
            V.pb(eval(nivel+1));
        }
         
        sort(V.begin(),V.end());
         
        r=V[0];
        for (size_t i=1;i<V.size();++i)
        {
            if(V[i].F!=V[i-1].S+1)
                ok=0;
            else
                r.S=V[i].S;
        }
        return r;
    }
    if (nivel==1)
    {
        Pair r,aux;
        for (r=eval(nivel+1);s[poz]=='>';)
        {
            ++poz;
            if (r.S!=(aux=eval(nivel+1)).F-1)
                ok=0;
            r.S=aux.S;
        }
        return r;
    }
    if (s[poz]=='(')
    {
        ++poz;
        Pair r=eval(0);
        ++poz;
        return r;
    }
    int x=0;
     
    while (s[poz]>='0'&&s[poz]<='9')
     
        x=(x<<3)+(x<<1)+s[poz++]-'0';
     
    return Mp(Ps[x],Ps[x]);
}
 
int main()
{
    freopen("episoade.in","r",stdin);
    freopen("episoade.out","w",stdout);
     
    fgets(s,sizeof(s),stdin);
    scanf("%d %d\n",&t,&n);
     
    while (t--)
    {
        poz=0;
         
        for(i=1;i<=n;++i)
        {
            scanf("%d",&j);
            Ps[j]=i;
        }
         
        ok=1;
        eval(0);
         
        printf("%d\n",ok);
    }
}