結果

問題 No.599 回文かい
ユーザー nebukuro09nebukuro09
提出日時 2017-11-24 23:54:06
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 1,685 ms / 4,000 ms
コード長 1,602 bytes
コンパイル時間 859 ms
コンパイル使用メモリ 96,152 KB
実行使用メモリ 189,152 KB
最終ジャッジ日時 2023-09-03 17:03:15
合計ジャッジ時間 10,613 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 4 ms
4,388 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,384 KB
testcase_07 AC 4 ms
4,384 KB
testcase_08 AC 4 ms
4,384 KB
testcase_09 AC 3 ms
4,380 KB
testcase_10 AC 626 ms
77,232 KB
testcase_11 AC 396 ms
57,980 KB
testcase_12 AC 724 ms
77,376 KB
testcase_13 AC 421 ms
57,728 KB
testcase_14 AC 1,202 ms
154,404 KB
testcase_15 AC 1,277 ms
185,792 KB
testcase_16 AC 1,455 ms
154,352 KB
testcase_17 AC 1,685 ms
189,152 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,384 KB
evil_0.txt AC 955 ms
154,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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;

const long MOD = 10^^9 + 7;
const long BASE = 97;


void main() {
    auto S = readln.chomp;
    auto N = S.length.to!int;

    auto powmod = new long[](N+1);
    powmod[0] = 1;
    foreach (i; 0..N)
        powmod[i+1] = powmod[i] * BASE % MOD;


    auto same = new bool[][](N+1, N/2);

    foreach (k; 1..N+1) {
        auto table = new long[](N-k+2);
        long hash = 0;
        foreach (i; 0..k) {
            hash = hash * BASE % MOD;
            hash = (hash + S[i]-'a') % MOD;
        }
        table[0] = hash;

        foreach (i; 1..N-k+1) {
            hash = hash - (S[i-1] - 'a') * powmod[k-1] % MOD;
            hash = hash * BASE % MOD;
            hash = (hash + S[i+k-1]-'a') % MOD;
            hash = (hash + MOD) % MOD;
            table[i] = hash;
        }

        foreach (i; 0..N/2)
            if (N-i-1-k+1 < 0)
                break;
            else
                same[k][i] = table[i] == table[N-i-1-k+1];
    }


    auto mem = new long[](N/2);
    mem.fill(-1);

    long dfs(int l) {
        if (l >= N/2)
            return 0;
        if (mem[l] >= 0)
            return mem[l];

        int r = N - l - 1;
        long ret = 0;

        foreach (len; 1..N) {
            if (l+len-1 >= N/2)
                break;
            if (same[len][l])
                (ret += dfs(l+len) + 1) %= MOD;
        }
        mem[l] = ret;
        return ret;
    }

    writeln((dfs(0)+1)%MOD);
}
0