Pagini recente » Cod sursa (job #2691187) | Cod sursa (job #1206217) | Cod sursa (job #2691244) | Cod sursa (job #2960927) | Cod sursa (job #996731)
Cod sursa(job #996731)
#include <iostream>
#include <fstream>
#include <cstring>
#include <list>
using namespace std;
int L[30];
string a;
string b;
int sum;
int last;
int valid()
{
for ( int i = 1; i <= 26; ++i )
if ( L[i] > 0 && i != last )
{
if ( L[i] >= sum/2 + 1 )
return i;
}
return -1;
}
int main()
{
ifstream f("ordine.in");
ofstream g("ordine.out");
f >> a;
sum = a.length();
for ( int i = 0; i < sum; ++i )
L[ a[i] - 96 ]++;
while( sum > 0 )
{
int elem = valid();
if ( elem == -1 )
{
for ( int i = 1; i <= 26; ++i )
if ( L[i] > 0 && i != last )
{
b.push_back( char( 96 + i ) );
L[i]--;
sum--;
last = i;
break;
}
}
else
{
b.push_back( char( 96 + elem ) );
L[elem]--;
sum--;
last = elem;
}
}
g << b << "\n";
f.close();
g.close();
return 0;
}