Cod sursa(job #632350)
Utilizator | Data | 10 noiembrie 2011 21:45:56 | |
---|---|---|---|
Problema | Tm | Scor | 0 |
Compilator | cpp | Status | done |
Runda | arhiva-teme-fmi | Marime | 3.48 kb |
#include<fstream>
#include<cstring>
#include<iostream>
using namespace std;
ifstream f("tm.in");
ofstream g("tm.out");
typedef struct turing{
char ch;
turing *next, *pred;
};
turing *Ma,*Mb,*Mc,*capA, *capB, *capC, *temp;
char Cuv[100];
int N;
void introdInBanda(turing *M,char c)
{
turing *m=M;
while( m->ch!='B')
m=m->next;
m->ch=c;
}
void initBanda(turing *&M)
{
if( M==NULL)
{
M=new turing;
M->ch='B';
M->pred=NULL;
M->next=NULL;
turing *m=M;
for( int i=1; i<=100;i++)
{
turing *temp= new turing;
temp->ch='B';
temp->pred=m;;
temp->next=NULL;
m->next=temp;
m=temp;
}
}
else
{
turing *m=M;
for( int i=1; i<=100; i++)
{
m->ch='B';
m=m->next;
}
}
}
int cuvBun( char Cuv[])
{
for( int i=0; i<=strlen(Cuv); i++)
if((Cuv[i]=='b' && Cuv[i+1]=='a') || (Cuv[i]=='c' && (Cuv[i+1]=='a' || Cuv[i+1]=='b')))
return 0;
return 1;
}
int main()
{
f>>N;
for( int i=1; i<=N; i++)
{
f>>Cuv;
initBanda(Ma);
initBanda(Mb);
initBanda(Mc);
if( cuvBun(Cuv))
{for( int j=0; j<strlen(Cuv); j++)
if( Cuv[i]=='a')
introdInBanda(Ma,'a');
else
{if(Cuv[i]=='b')
introdInBanda(Mb,'b');
else
introdInBanda(Mc,'c');}
capA=Ma;
capB=Mb;
capC=Mc;
while( capA->next->ch!='B' || capB->next->ch!='B' || capC->next->ch!='B')
{
while( capA->ch=='a' || capB->ch=='b')
{
capA->ch='x';
capB->ch='y';
}
if( capA->ch == 'B')
{ if( capB->ch == 'B' )
g<<"DA\n";
else
{
while(capB->pred->pred!=NULL)
{capB->ch='b';
capB=capB->pred;
}
capB->ch='b';
while( capC->ch=='c' || capB->ch=='b')
{
capC->ch='x';
capB->ch='y';
}
if ( capB->ch=='B')
{if ( capC->ch=='B' )
g<<"DA\n";
else
g<<"NU\n";}
else
cout<<"NU\n";
}
}
}
}
else
g<<"NU\n";
}
return 1;
}