Cod sursa(job #1376522)

Utilizator andreiionutBude Andrei-Ionut andreiionut Data 5 martie 2015 17:40:49
Problema Perle Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.05 kb
#include <iostream>
#include <fstream>
#include <cstring>
#define fin "perle.in"
#define fou "perle.out"
using namespace std;
struct bile{int l,perlute[10001];} c[11];
char x[10001];
int n;

int comparare(int nr)
{
    int i,ok=1;
    for(i=1;i<=c[nr].l;i++) if(c[nr].perlute[i]!=int(x[i-1])-48) ok=0;
    if(strlen(x)!=c[nr].l) ok=0;
    return ok;
}

void completare (int nr,int poz)
{
    char y[10001];
    if(x[poz]=='A') x[poz]=char(c[nr].perlute[poz+1]+48);
    if(x[poz]=='B')
        if(c[nr].perlute[poz+1]==2) { strcpy(y,x+poz+1); strcpy(x+poz,""); strcpy(x+poz,"2B");  strcpy(x+poz+2,y);}
        else
        if(c[nr].perlute[poz+1]==1) { strcpy(y,x+poz+1); strcpy(x+poz,""); strcpy(x+poz,"1A3AC"); strcpy(x+poz+5,y);}
    if(x[poz]=='C')
        if(c[nr].perlute[poz+1]==2) x[poz]='2';
        else
        if(c[nr].perlute[poz+1]==1) { strcpy(y,x+poz+1); strcpy(x+poz,""); strcpy(x+poz,"12A"); strcpy(x+poz+3,y);}
        else
        if(c[nr].perlute[poz+1]==3) { strcpy(y,x+poz+1); strcpy(x+poz,""); strcpy(x+poz,"3BC"); strcpy(x+poz+3,y);}
}

int constructie(int nr)
{
    int poz,i;
    poz=1;
    while(poz<=c[nr].l)
    {
        completare(nr,poz);
        poz++;
    }
}

int main()
{
    int i,j,lon;
    ifstream t1(fin);
    ofstream t2(fou);
    t1>>n;
    for(i=1;i<=n;i++)
    {
        t1>>lon; c[i].l=lon;
        for(j=1;j<=lon;j++) t1>>c[i].perlute[j];
    }
    for(i=1;i<=n;i++)
    {
        strcpy(x,"");
        if(c[i].l==1) t2<<'1'<<'\n';
        else
        if(c[i].l==2) t2<<'0'<<'\n';
        else
        if(c[i].l==3)
            if(c[i].perlute[1]==1 && c[i].perlute[2]==2) t2<<'1'<<'\n';
            else t2<<'0'<<'\n';
        else
        {
            if(c[i].perlute[1]==1) strcat(x,"1A3AC");
            if(c[i].perlute[1]==2) strcat(x,"2B");
            if(c[i].perlute[1]==3) strcat(x,"3BC");
            constructie(i);
            if(comparare(i)==1) t2<<'1'<<'\n';
            else t2<<'0'<<'\n';
        }
    }
    t1.close();
    t2.close();
    return 0;
}