結果
問題 | No.765 ukuku 2 |
ユーザー |
|
提出日時 | 2018-12-13 14:06:35 |
言語 | D (dmd 2.109.1) |
結果 |
AC
|
実行時間 | 418 ms / 3,000 ms |
コード長 | 2,464 bytes |
コンパイル時間 | 818 ms |
コンパイル使用メモリ | 117,708 KB |
実行使用メモリ | 10,344 KB |
最終ジャッジ日時 | 2024-06-13 02:19:27 |
合計ジャッジ時間 | 8,637 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 48 |
ソースコード
import std.stdio, std.array, std.string, std.conv, std.algorithm;import std.typecons, std.range, std.random, std.math, std.container;import std.numeric, std.bigint, core.bitop, core.stdc.string;const long MOD = 10^^9 + 9;const long BASE = 31;void main() {auto S = readln.chomp;auto N = S.length.to!int;auto T = S.to!(dchar[]).reverse.to!string;if (is_kaibun(S)) {writeln(N-1);return;}auto P = new long[](N+1);auto P_INV = new long[](N+1);P[0] = P_INV[0] = 1;foreach (i; 0..N) P[i+1] = P[i] * BASE % MOD;foreach (i; 0..N) P_INV[i] = powmod(P[i], MOD-2, MOD);auto rh = new long[](N+1);rh[N] = 0;foreach (i; 0..N) rh[i+1] = (rh[i] + (S[i] - 'a') * P[i]) % MOD;auto hr = new long[](N+1);hr[N] = 0;foreach (i; 0..N) hr[i+1] = (hr[i] + (T[i] - 'a') * P[i]) % MOD;long hash_substr(int l, int r) { // [l, r]return ((rh[r+1] - rh[l]) * P_INV[l] % MOD + MOD) % MOD;}long hash_reverse(int l, int r) {int nl = N - r - 1;int nr = N - l - 1;return ((hr[nr+1] - hr[nl]) * P_INV[nl] % MOD + MOD) % MOD;}int bin_search(int r1, int l2) {if (r1 < 0 || l2 >= N)return -1;int hi = min(r1+1, N-l2);int lo = -1;while (hi - lo > 1) {int mid = (hi + lo) / 2;int l1 = r1 - mid;int r2 = l2 + mid;if (hash_substr(l1, r1) == hash_reverse(l2, r2))lo = mid;elsehi = mid;}return lo;}int ans = 0;foreach (i; 0..N) { // S[i]が中心int len1 = bin_search(i-1, i+1) + 1;int r1 = i - len1 - 1;int l2 = i + len1 + 1;int len2 = max(bin_search(r1-1, l2), bin_search(r1, l2+1)) + 1;ans = max(ans, len1 * 2 + len2 * 2 + 1);}foreach (i; 0..N-1) { // S[i..i+1]が中心int len1 = bin_search(i, i+1) + 1;int r1 = i - len1;int l2 = i + len1 + 1;int len2 = max(bin_search(r1-1, l2), bin_search(r1, l2+1)) + 1;ans = max(ans, len1 * 2 + len2 * 2);}ans.writeln;}bool is_kaibun(string s) {return iota(s.length/2).map!(i => s[i] == s[s.length.to!int-i-1]).all;}long powmod(long a, long x, long m) {long ret = 1;while (x) {if (x % 2) ret = ret * a % m;a = a * a % m;x /= 2;}return ret;}