結果

問題 No.3001 ヘビ文字列
ユーザー 👑 hos.lyric
提出日時 2025-01-01 08:09:30
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 224 ms / 1,500 ms
コード長 2,457 bytes
コンパイル時間 3,395 ms
コンパイル使用メモリ 118,488 KB
実行使用メモリ 15,940 KB
最終ジャッジ日時 2025-01-01 08:09:52
合計ジャッジ時間 21,319 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 83
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }

string COLOR(string s = "") { return "\x1b[" ~ s ~ "m"; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }




void main() {
  try {
    for (; ; ) {
      const S = readToken;
      const N = cast(int)(S.length);
      
      auto lpf = new int[N + 1];
      foreach (p; 2 .. N + 1) lpf[p] = p;
      foreach (p; 2 .. N + 1) if (lpf[p] == p) {
        for (int n = p; n <= N; n += p) chmin(lpf[n], p);
      }
      
      int[26] cnt;
      
      int opt = N + 1;
      int dm = -1;
      foreach (p; 2 .. N + 1) if (lpf[p] == p) if (N % p == 0) {
        const d = N / p;
        int cost;
        foreach (r; 0 .. d) {
          int mx;
          for (int i = r; i < N; i += d) chmax(mx, ++cnt[S[i] - 'A']);
          cost += (N/d - mx);
          for (int i = r; i < N; i += d) --cnt[S[i] - 'A'];
        }
        if (chmin(opt, cost)) {
          dm = d;
        }
      }
      debug writefln("opt = %s, dm = %s", opt, dm);
      
      auto ans = new char[N];
      {
        const d = dm;
        foreach (r; 0 .. dm) {
          for (int i = r; i < N; i += d) ++cnt[S[i] - 'A'];
          int am;
          foreach (a; 0 .. 26) if (cnt[am] < cnt[a]) am = a;
          for (int i = r; i < N; i += d) ans[i] = cast(char)('A' + am);
          for (int i = r; i < N; i += d) --cnt[S[i] - 'A'];
        }
      }
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0