結果
| 問題 |
No.3001 ヘビ文字列
|
| ユーザー |
addeight2
|
| 提出日時 | 2024-12-21 05:53:15 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,484 bytes |
| コンパイル時間 | 1,998 ms |
| コンパイル使用メモリ | 174,160 KB |
| 実行使用メモリ | 13,772 KB |
| 最終ジャッジ日時 | 2025-01-01 07:32:15 |
| 合計ジャッジ時間 | 134,910 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 23 WA * 20 TLE * 40 |
ソースコード
#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;
ll solve_w_period(string &S, ll period, bool replace_S = false) {
ll n = S.length(), ans = 0;
REP(i, period) {
map<char, ll> char_cnt;
REP(j, n / period) {
++char_cnt[S[i + period * j]];
}
auto max_cnt = *max_element(char_cnt.begin(), char_cnt.end(), [](auto a, auto b) { return a.second < b.second; });
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 (period != 1) {
temp_ans = solve_w_period(S, n / period);
if (temp_ans < ans) {
ans = temp_ans;
best_period = 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;
}
addeight2