Pagini recente » Cod sursa (job #126033) | Cod sursa (job #1553948) | Monitorul de evaluare | Cod sursa (job #2425784) | Cod sursa (job #2024697)
#include <cstdio>
#include <cstring>
const int MAXN = 1e6;
const int ALF = 26;
int fr[ALF];
char s[MAXN + 1], ans[MAXN + 1];
int main() {
int n;
bool check;
FILE *f = fopen("ordine.in", "r");
fscanf(f, "%s", s);
fclose(f);
n = strlen(s);
for (int i = 0; i <= n; ++i) {
++fr[s[i] - 'a'];
}
for (int i = 1; i <= n; ++i) {
check = 0;
for (int j = 0; j < ALF && !check; ++j) {
if (fr[j] && ((n - i + 1) >> 1) + 1 <= fr[j] && ans[i - 1] != j + 'a') {
ans[i] = j + 'a';
--fr[j];
check = 1;
}
}
if (!check) {
for (int j = 0; j < ALF && !check; ++j) {
if (fr[j] && ans[i - 1] != j + 'a') {
ans[i] = j + 'a';
--fr[j];
check = 1;
}
}
}
}
ans[n + 1] = '\0';
f = fopen("ordine.out", "w");
fprintf(f, "%s\n", ans + 1);
fclose(f);
return 0;
}