Cod sursa(job #529972)

Utilizator tudorsTudor Siminic tudors Data 6 februarie 2011 16:53:49
Problema Ordine Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.92 kb
#include <fstream>
#include <string>
using namespace std;
int n,ma;
char c;
int i,j;
int A[40];

int urm(int de_unde)
{
	int i;
	for (i=de_unde+1;i<=40;i++)
		if (A[i])
			return i;
	return 0;
}

int main()
{
	ifstream f("ordine.in");
	ofstream g("ordine.out");
	f>>c;
	while (!f.eof())
	{
		A[c-'a']++;
		f>>c;
	}
	i=urm(-1);
	j=urm(i);
	if (j==0)
		while (A[i])
		{
			g<<char(i+'a');
			A[i]--;
		}
	ma=j;
	while (ma!=0)
	{
		while (A[i] && A[j])
		{
			g<<char(i+'a');
			g<<char(j+'a');
			A[i]--;
			A[j]--;
		}
		if (!A[i])
		{
			i=urm(ma);
			ma=i;
			if (!i && urm(j))
			{
				while (A[j]!=0)
				{
					g<<char(j+'a');
					A[j]--;
				}
				return 0;
			}
		}
		if (!A[j])
		{
			j=urm(ma);
			ma=j;
			if (!j && i)
			{
				while (A[i]!=0)
				{
					g<<char(i+'a');
					A[i]--;
				}
				return 0;
			}
		}
	}
	f.close();
	g.close();
	return 0;
}