結果

問題 No.3001 ヘビ文字列
ユーザー addeight2
提出日時 2024-12-22 20:53:59
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,659 bytes
コンパイル時間 2,981 ms
コンパイル使用メモリ 173,108 KB
実行使用メモリ 39,656 KB
最終ジャッジ日時 2025-01-01 07:38:20
合計ジャッジ時間 167,709 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 23 TLE * 60
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define REP(i, a) FOR(i, 0, a)

using namespace std;

vector<int> get_p_factors(int n) {
    vector<int> 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;
}

pair<int, string> solve_w_period(string S, int period) {
    int n = S.length(), ans = 0;
    vector<int> char_cnt_vec(n, 0);
    REP(i, period) {
        pair<char, int> max_cnt;
        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];
        fill(char_cnt_vec.begin(), char_cnt_vec.end(), 0);
        ans += n / period - max_cnt.second;
        REP(j, n / period) {
            S[i + period * j] = max_cnt.first;
        }
    }
    return make_pair(ans, S);
}

string solve(string S) {
    int n = S.length();
    vector<int> p_factors = get_p_factors(n);

    auto ans = make_pair(n, S);
    for (int p_factor : p_factors) {
        int period = n / p_factor;
        auto temp_ans = solve_w_period(S, period);
        if (temp_ans.first < ans.first) {
            ans = temp_ans;
        }
    }
    return ans.second;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    string S;
    cin >> S;
    cout << solve(S) << endl;
}
0