#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; vector soinsuu_bunkai(ll n) { vector soinsuu; for (ll i = 2; i * i <= n; ++i) { if (n % i == 0) { soinsuu.push_back(i); while (n % i == 0) { n /= i; } } } if (n != 1) { // root(n) までで割り切れなければ素数 soinsuu.push_back(n); } return soinsuu; } ll solve_period(string &S, ll period, bool replace_S = false) { // 周期が period の文字列にするためにかかるコストを返す ll n = S.length(), ans = 0; REP(i, period) { vector char_cnt('Z' - 'A' + 1, 0); REP(j, n / period) { ++char_cnt[S[i + period * j] - 'A']; } auto max_ele = max_element(char_cnt.begin(), char_cnt.end()); ans += n / period - *max_ele; if (replace_S) { REP(j, n / period) { S[i + period * j] = 'A' + (max_ele - char_cnt.begin()); } } } return ans; } void solve(string &S) { ll n = S.length(); vector soinsuu_vec = soinsuu_bunkai(n); ll ans = n, best_period = 1; for (ll soinsuu : soinsuu_vec) { ll period = n / soinsuu; ll temp_ans = solve_period(S, period); if (temp_ans < ans) { ans = temp_ans; best_period = period; } } solve_period(S, best_period, true); } int main() { string S; cin >> S; solve(S); cout << S << endl; }