#include #define FOR(i, a, b) for(ll i = (a); i < (b); ++i) #define REP(i, a) FOR(i, 0, a) using namespace std; using ll = long long; ll solve_w_period(string &S, ll period, bool replace_S = false) { ll n = S.length(), ans = 0; REP(i, period) { vector char_cnt_vec('Z' - 'A' + 1, 0); pair max_cnt; REP(j, n / period) { ++char_cnt_vec[S[i + period * j] - 'A']; } ll max_ele = max_element(char_cnt_vec.begin(), char_cnt_vec.end()) - char_cnt_vec.begin(); max_cnt.first = 'A' + max_ele; max_cnt.second = char_cnt_vec[max_ele]; ans += n / period - max_cnt.second; if (replace_S) { REP(j, n / period) { S[i + period * j] = max_cnt.first; } } } return ans; } void solve(string &S) { ll n = S.length(); ll ans = n, best_period = 1; for (ll period = 1; period * period <= n; ++period) { if (n % period == 0) { ll temp_ans = solve_w_period(S, period); if (temp_ans < ans) { ans = temp_ans; best_period = period; } if (n / period != period && period != 1) { temp_ans = solve_w_period(S, n / period); if (temp_ans < ans) { ans = temp_ans; best_period = n / period; } } } } solve_w_period(S, best_period, true); } int main() { cin.tie(0); ios::sync_with_stdio(false); string S; cin >> S; solve(S); cout << S << endl; }