Cod sursa(job #532173)

Utilizator ChallengeMurtaza Alexandru Challenge Data 10 februarie 2011 22:33:17
Problema Ordine Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <fstream>
#include <cstring>

using namespace std;

const char InFile[]="ordine.in";
const char OutFile[]="ordine.out";
const int MaxN=1000111;
const int SIGMA=26;

ifstream fin(InFile);
ofstream fout(OutFile);

char buff[MaxN];
int c[SIGMA],N;

int main()
{
	fin>>buff;
	fin.close();

	N=strlen(buff);
	for(register int i=0;i<N;++i)
	{
		++c[buff[i]-'a'];
	}

	int last=SIGMA;
	for(register int i=0;i<N;++i)
	{
		bool found=false;
		for(register int j=0;j<SIGMA;++j)
		{
			if(c[j]==(N-i)/2+1 && last!=j)
			{
				--c[j];
				fout<<(char)(j+'a');
				last=j;
				found=true;
				break;
			}
		}
		if(!found)
		{
			for(register int j=0;j<SIGMA;++j)
			{
				if(c[j]>0 && last!=j)
				{
					--c[j];
					fout<<(char)(j+'a');
					last=j;
					break;
				}
			}
		}
	}
	fout.close();
	return 0;
}