Cod sursa(job #632462)

Utilizator anuska_26Turlea Ana Cristina anuska_26 Data 11 noiembrie 2011 10:44:38
Problema Tm Scor 90
Compilator cpp Status done
Runda arhiva-teme-fmi Marime 4.17 kb
#include<fstream>
#include<cstring>
#include<iostream>
using namespace std;
ifstream f("tm.in");
ofstream g("tm.out");

 struct  turing{
	   char ch;
		turing *next, *pred;
       };
turing *Ma,*Mb,*Mc,*capA, *capB, *capC, *temp;
	
char Cuv[1000];
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<=1000;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<=1000; i++)
         {
              m->ch='B';
              m=m->next;
              
              }
     }
     
}

void afisBanda( turing *M)
{
     while(M->next!=0)
     {
                      cout<<M->ch;
                      M=M->next;
                      }
     cout<<endl;
     }

int cuvBun( char Cuv[])
{
    for( unsigned 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()
{
    Ma=NULL;
    Mb=NULL;
    Mc=NULL;
    f>>N;
    for( int i=1; i<=N; i++)
         {
             f>>Cuv;
            
             initBanda(Ma);
             initBanda(Mb);
             initBanda(Mc);
           
             if( cuvBun(Cuv))
             {for( unsigned int j=0; j<strlen(Cuv); j++)
             
                  if( Cuv[j]=='a')
                      introdInBanda(Ma,'a');
                  else
                  {if(Cuv[j]=='b')
                     introdInBanda(Mb,'b');
                     else
                     introdInBanda(Mc,'c');
                     }
              capA=Ma;
              capB=Mb;
              capC=Mc;
              
                    while( capA->ch=='a' && capB->ch=='b')
                    {
                           capA->ch='x';
                           capB->ch='y';
                           capA=capA->next;
                           capB=capB->next;                        
                           }
                    
                     if( capA->ch == 'B' && capB->ch == 'B' ) 
                             g<<"DA\n";
                            
                         else
                       
                       
                       if((capA->ch != 'B' && capB->ch == 'B' ) || (capA->ch == 'B' && capB->ch != 'B'))
                        
                             { 
                                while(capB->pred!=NULL)
                                     {
                                           if(capB->ch=='y')
                                              capB->ch='b';
                                      capB=capB->pred;
                                      }
                                capB->ch='b';
                                
                                while( capC->ch=='c' && capB->ch=='b')
                                {
                                       capC->ch='x';
                                       capB->ch='y'; 
                                       capB=capB->next;
                                       capC=capC->next;                        
                                       }     
                                       
                                if ( capB->ch=='B' && capC->ch=='B'  )
                                  
                                     g<<"DA\n";
                                    else
                                    
                                    g<<"NU\n";
                                       
                                
                                
                                }   
                                }
                
			 else
				 g<<"NU\n";
            
         }   
    


    return 0;
    
    }