#include #define FOR(i, a, b) for(int i = (a); i < (b); ++i) #define REP(i, a) FOR(i, 0, a) using namespace std; vector get_p_factors(int n) { vector p_factors; for (int i = 2; i * i <= n; ++i) { if (n % i == 0) { p_factors.push_back(i); while (n % i == 0) { n /= i; } } } if (n != 1) { p_factors.push_back(n); } return p_factors; } int solve_w_period(string &S, int period, bool replace_S = false) { int n = S.length(), ans = 0; vector char_cnt_vec; bool flg = (n / period * 2 < 13); if (!flg) { char_cnt_vec.resize('Z' - 'A' + 1); } REP(i, period) { map char_cnt; pair max_cnt; if (flg) { REP(j, n / period) { ++char_cnt[S[i + period * j]]; } max_cnt = *max_element(char_cnt.begin(), char_cnt.end(), [](auto a, auto b) { return a.second < b.second; }); }else{ REP(j, n / period) { ++char_cnt_vec[S[i + period * j] - 'A']; } int 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]; if (n / period < 26){ REP(j, n / period) char_cnt_vec[S[i + period * j] - 'A'] = 0; }else{ fill(char_cnt_vec.begin(), char_cnt_vec.end(), 0); } } 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) { int n = S.length(); vector p_factors = get_p_factors(n); int ans = n, best_period = 1; for (int p_factor : p_factors) { int period = n / p_factor; int temp_ans = solve_w_period(S, period); if (temp_ans < ans) { ans = temp_ans; best_period = period; } } solve_w_period(S, best_period, true); } int main() { string S; cin >> S; solve(S); cout << S << endl; }