結果

問題 No.599 回文かい
ユーザー rogi52rogi52
提出日時 2022-02-21 19:17:30
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 630 ms / 4,000 ms
コード長 3,384 bytes
コンパイル時間 2,934 ms
コンパイル使用メモリ 205,224 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-12 10:44:34
合計ジャッジ時間 6,502 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 268 ms
4,376 KB
testcase_11 AC 157 ms
4,380 KB
testcase_12 AC 304 ms
4,384 KB
testcase_13 AC 177 ms
4,380 KB
testcase_14 AC 455 ms
4,380 KB
testcase_15 AC 525 ms
4,376 KB
testcase_16 AC 550 ms
4,376 KB
testcase_17 AC 630 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
evil_0.txt AC 380 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

struct RollingHash {
    static constexpr int M = 2;
    static constexpr long long MODS[M] = {999999937, 1000000007};
    static constexpr long long BASE = 9973;
    vector<long long> powb[M], hash[M];
    int n;
    RollingHash() {}
    RollingHash(const string& str) { init(str); }
    void init(const string& str) {
        n = str.size();
        for (int k = 0; k < M; k++) {
            powb[k].resize(n + 1, 1);
            hash[k].resize(n + 1, 0);
            for (int i = 0; i < n; i++) {
                hash[k][i + 1] = (hash[k][i] * BASE + str[i]) % MODS[k];
                powb[k][i + 1] = powb[k][i] * BASE % MODS[k];
            }
        }
    }
    // get hash str[l,r)
    long long get(int l, int r, int k) {
        long long res = hash[k][r] - hash[k][l] * powb[k][r - l] % MODS[k];
        if (res < 0) res += MODS[k];
        return res;
    }
};

bool match(RollingHash& rh1, int l1, int r1, RollingHash& rh2, int l2, int r2) {
    bool res = true;
    for (int k = 0; k < RollingHash::M; k++) {
        res &= rh1.get(l1, r1, k) == rh2.get(l2, r2, k);
    }
    return res;
}

template< int mod >
struct Fp {
    int x;
    Fp() : x(0) {}
    Fp(int64_t y) : x(y >= 0 ? y % mod : (mod + y % mod) % mod) {}
    Fp &operator+=(const Fp &p) { if((x += p.x) >= mod) x -= mod; return *this; }
    Fp &operator-=(const Fp &p) { if((x += mod - p.x) >= mod) x -= mod; return *this; }
    Fp &operator*=(const Fp &p) { x = (int)(1LL * x * p.x % mod); return *this; }
    Fp &operator/=(const Fp &p) { *this *= p.inv(); return *this; }
    Fp operator-() const { return Fp(-x); }
    Fp operator+(const Fp &p) const { return Fp(*this) += p; }
    Fp operator-(const Fp &p) const { return Fp(*this) -= p; }
    Fp operator*(const Fp &p) const { return Fp(*this) *= p; }
    Fp operator/(const Fp &p) const { return Fp(*this) /= p; }
    bool operator==(const Fp &p) const { return x == p.x; }
    bool operator!=(const Fp &p) const { return x != p.x; }

    Fp inv() const {
        int a = x, b = mod, u = 1, v = 0, t;
        while(b > 0) {
            t = a / b;
            swap(a -= t * b, b);
            swap(u -= t * v, v);
        }
        return Fp(u);
    }

    Fp pow(int64_t n) const {
        Fp ret(1), mul(x);
        while(n > 0) {
            if(n & 1) ret *= mul;
            mul *= mul;
            n >>= 1;
        }
        return ret;
    }

    friend ostream &operator<<(ostream &os, const Fp &p) { return os << p.x; }
    friend istream &operator>>(istream &is, Fp &a) {
        int64_t t;
        is >> t;
        a = Fp< mod > (t);
        return (is);
    }

    static int get_mod() { return mod; }
};

using mint = Fp<1000000007>;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    string S; cin >> S;
    int N = S.size();

    RollingHash H(S);
    // dp[i] = S[i,N-i) の分解の通り数
    vector<mint> dp(N / 2 + 1);
    dp[N / 2] = 1;
    for(int i = N / 2 - 1; i >= 0; i--) {
        // dp[i] = 1 + sum(dp[L] | i < L <= N/2 and S[i,L) == S[N-L,N-i))
        for(int L = i + 1; L <= N / 2; L++) {
            if(match(H, i, L, H, N - L, N - i)) {
                dp[i] += dp[L];
            }
        }
        dp[i] += 1;
    }

    // dp[0] = S[0, N) の通り数
    cout << dp[0] << endl;
}
0