Cod sursa(job #1833779)

Utilizator tamionvTamio Vesa Nakajima tamionv Data 23 decembrie 2016 08:25:46
Problema Ordine Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.84 kb
#include <bits/stdc++.h>
using namespace std;

ifstream f("ordine.in");
ofstream g("ordine.out");

string str;
int frec[256] = {};
list<char> ls;

bool append(){
	for(char ch = 'a'; ch <= 'z'; ++ch){
		if(!ls.empty() && ls.back() == ch) continue;
		if(frec[ch] == 0) continue;
		ls.push_back(ch);
		--frec[ch];
		return true; }
	return false; }

int main(){
	f >> str;
	for(const auto x : str) ++frec[x];

	while(append());

	if(all_of(begin(frec), end(frec), [](const int x){ return x == 0; })){
		for(const auto x : ls) g << x;
		return 0; }

	char ch = 'a';
	while(!frec[ch]) ++ch;
	
	reverse(begin(ls), end(ls));
	for(auto it = ++begin(ls), prev_it = begin(ls); frec[ch]; ){
		if(*prev_it != ch && *it != ch){
			it = ls.insert(it, ch);
			--frec[ch]; }
		else{
			++it, ++prev_it; } }
	reverse(begin(ls), end(ls));
	for(const auto x : ls) g << x;
	return 0; }