Cod sursa(job #632494)

Utilizator VladberilaVladutz Vladberila Data 11 noiembrie 2011 12:59:07
Problema Tm Scor 100
Compilator cpp Status done
Runda arhiva-teme-fmi Marime 2.93 kb
//simulare a masinii turing fmm

#include <list>
#include <iterator>
#include <fstream>
#include <cstring>
using namespace std;
ifstream f("tm.in");
ofstream g("tm.out");
int solve()
{
	list<char> L;
	list<char>::iterator it;
	char s[1002];
	int i,m;
	f>>s;
	m=strlen(s);
	for(i=0;i<m;i++)
		L.push_back(s[i]);
	for(it=L.begin();it!=L.end() && (char)(*it)=='a';it++);
	for(;it!=L.end() && (char)(*it)=='b';it++);
	for(;it!=L.end() && (char)(*it)=='c';it++);
	if(it!=L.end())
	{
		g<<"NU";
		return 0;
	}
		it=L.begin();
		while(it!=L.end() && (char)(*it)=='a')
		{
			*it='d';//marchez a
			for(;it!=L.end() && (char)(*it)!='b';it++);
			if(it!=L.end())
				*it='e';//marchez b
			else//daca nu exista b demarchez toate e-urile
			{
				for(it=L.begin();it!=L.end() && (char)(*it)!='e';it++);
				if(it==L.end()) //nu exita b deloc -> accept -> b=c=0
				{
					g<<"DA\n";
					return 0;
				}
				for(;it!=L.end() && (char)(*it)=='e';it++)
					*it='b';
				for(it=L.begin();it!=L.end() && (char)(*it)!='b';it++);
	        	for(;it!=L.end() && (char)(*it)=='b';it++)
	        	{
	        		if(it!=L.end())
					{
		            	*it='e';//marchez un b
		            	for(;it!=L.end() && (char)(*it)!='c';it++);
		            	if(it==L.end())//nu exista c resping
			            {
			             	g<<"NU\n";
			            	return 0;
		            	}
		            	else
			             	*it='f'; //marchez un c
		            	for(it=L.begin();it!=L.end() && (char)(*it)!='b';it++);
			        	it--;
		            }
	        	}
	        	for(it=L.begin();it!=L.end();it++) //daca nu exista c acceptam altfel respingem
		        	if((char)(*it)=='c')
		        	{
			        	g<<"NU\n";
                        return 0;
					}
	         	g<<"DA\n";
                return 0;
			}
			for(it=L.begin();it!=L.end() && (char)(*it)!='a';it++);
		}
		//daca nu exista a
			int okb=1;
			for(it=L.begin();it!=L.end() && okb;it++) //daca nu exista b acceptam
				if((char)(*it)=='b')
					okb=0;
			if(okb)
			{
				g<<"DA\n";
				return 0;
			}
		for(it=L.begin();it!=L.end() && (char)(*it)!='e';it++); //demarchez b`urile
		for(;it!=L.end() && (char)(*it)=='e';it++)
			*it='b';
		for(it=L.begin();it!=L.end() && (char)(*it)!='b';it++);
		for(;it!=L.end() && (char)(*it)=='b';it++)
		{
			if(it!=L.end())
			{
		    	*it='e';//marchez un b
		    	for(;it!=L.end() && (char)(*it)!='c';it++);
		    	if(it==L.end())//nu exista c resping
			    {
			    	g<<"NU\n";
			    	return 0;
		    	}
		    	else
			     	*it='f'; //marchez un c
		    	for(it=L.begin();it!=L.end() && (char)(*it)!='b';it++);
				it--;
		    }
		}
		for(it=L.begin();it!=L.end();it++) //daca nu exista c acceptam altfel respingem
			if((char)(*it)=='c')
			{
				g<<"NU\n";
				return 0;
			}
		g<<"DA\n";

}
int main()
{
	int T;
	f>>T;
	for(int i=1;i<=T;i++)
		solve();
	f.close();
	g.close();
	return 0;
}