Cod sursa(job #340844)

Utilizator szabotamasSzabo Tamas szabotamas Data 16 august 2009 19:36:18
Problema Ordine Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>

#define MAXC 27

using namespace std;

long xx,v[MAXC],ok,ok2,q,qq,i,st1,st2,k;
string s;

void sw(char &a, char &b){
	char x;
	x=a;
	a=b;
	b=x;
}

int main(){
	ifstream fin("ordine.in");
	ofstream fout("ordine.out");
	fin >> s;
	for (i=0; i<=(s.length()); i++){
		v[int(s[i])-96]++;
	}
	st1=1;
	st2=2;
	q=0;
	qq=0;
	ok2=0;
	long a=s.length();
 	while (st1<27 || st2<27){
		q++;
		qq++;
		ok=0;
		if(qq%2==1){
			while((v[st1]==0 && st1<27) || (st1==st2 && st1<27)) ++st1;
			if(st1<27){
				s[q-1]=char(st1+96);
				v[st1]--;
				ok=1;
				if (ok2==0) for (i=1; i<=26; i++){
					if(v[i]>=(a-q)/2+1){
						if(st2<st1){ xx=st2; st2=i; st1=xx;}
						else {st2=i;}
						ok2=1;
						break;
					}
				}
			}
		}
		if(ok==0){
			while((v[st2]==0 && st2<27) || (st1==st2 && st2<27)) ++st2;
			if(st2<27){
				s[q-1]=char(st2+96);
				v[st2]--;
				ok=1;
				if (ok2==0) for (i=1; i<=26; i++){
					if(v[i]>=(a-q)/2+1){
						if(st1<st2){ xx=st1; st1=i; st2=xx;}
						else {st1=i;}
						ok2=1;
						break;
					}
				}
			}
		}
		if(ok==0) q--;
	}
	fout << s;
	return 0;
}