Cod sursa(job #632369)

Utilizator VladberilaVladutz Vladberila Data 10 noiembrie 2011 22:03:08
Problema Tm Scor 90
Compilator cpp Status done
Runda arhiva-teme-fmi Marime 2.67 kb
// tm_list.cpp : Defines the entry point for the console application.
//

#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,ok=1;
	f>>s;
	m=strlen(s);
	for(i=0;i<m;i++)
		L.push_back(s[i]);
	for(it=L.begin();it!=L.end() && ok;it++)   //verific daca apartine limbajului
	{
		if((char)(*it)=='a')
		{
			if(++it!=L.end() && (char)(*it)!='a')
			{
				it--;
				if((char)(*(++it))!='b')
					ok=0;
				it--;
			}
			else
				it--;
		}
		else
			if((char)(*it)=='b')
		    {
		    	if(++it!=L.end() && (char)(*it)!='b')
		    	{
				    it--;
			    	if((char)(*(++it))!='c')
			    		ok=0;
				    it--;
		    	}
				else
					it--;
	    	}
			else
			{
				if(++it!=L.end() && (char)(*it)!='c')
		    	{
				    ok=0;
					it--;
		    	}
				else
					it--;
			}
   	}
	if(ok)   //apartine limbajului..marcarea va fi a cu d, b cu e, c cu f
	{
		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)!='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--;
		    }
		}
		int okc=0;
		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";

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