結果

問題 No.3001 ヘビ文字列
ユーザー addeight2
提出日時 2025-01-01 11:38:27
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 381 ms / 1,500 ms
コード長 1,629 bytes
コンパイル時間 2,081 ms
コンパイル使用メモリ 172,376 KB
実行使用メモリ 9,104 KB
最終ジャッジ日時 2025-01-01 11:38:56
合計ジャッジ時間 27,612 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 83
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#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<ll> soinsuu_bunkai(ll n) {
    vector<ll> 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<ll> 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<ll> 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;
}
0