Cod sursa(job #625414)

Utilizator Magnuscont cu nume gresit sau fals Magnus Data 24 octombrie 2011 17:03:13
Problema Episoade Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.51 kb
#include<algorithm>
#include<cstdio>
#include<vector>

using namespace std;

struct str{int x,y;};
struct cmp_str{bool operator()(str x,str y){return x.x<y.x;};};
inline str mp(int x,int y){str aux;aux.x=x;aux.y=y;return aux;}
char s[1025];
int n,t,i,j,p,pos[1025],ok,poz=0;

str eval(int lvl)
{
    vector<str> v;
    vector <str>::iterator it;
    str r,aux;
    if(lvl==0)
    {
        for(v.push_back(eval(lvl+1));s[poz]=='#';)
            ++poz,v.push_back(eval(lvl+1));
        sort(v.begin(),v.end(),cmp_str());
        r=v[0];
        for(it=v.begin();it!=v.end();++it)
            if(it->x!=(it-1)->y+1)
                ok=0;
            else
                r.y=it->y;
        return r;
    }
    if(lvl==1)
    {
        for(r=eval(lvl+1);s[poz]=='>';)
        {
            ++poz;
            if(r.y!=(aux=eval(lvl+1)).x-1)
                ok=0;
            r.y=aux.y;
        }
        return r;
    }
    if(s[poz]=='(')
    {
        ++poz;
        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(pos[x],pos[x]);
}

int main()
{
    freopen("episoade.in","r",stdin);
    freopen("episoade.out","w",stdout);
    fgets(s,sizeof(s),stdin);
    scanf("%d%d",&t,&n);
    for(;t;--t)
    {
        poz=0;
        for(i=1;i<=n;++i)
            scanf("%d",&j);
        pos[j]=i;
        ok=1;
        eval(0);
        printf("%d\n",ok);
    }
    return 0;
}