結果

問題 No.599 回文かい
ユーザー veqccveqcc
提出日時 2019-07-21 14:27:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,425 bytes
コンパイル時間 1,198 ms
コンパイル使用メモリ 86,928 KB
実行使用メモリ 4,740 KB
最終ジャッジ日時 2023-09-03 17:59:52
合計ジャッジ時間 3,257 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
using namespace std;
typedef long long ll;

class RollingHash {
    const ll base = 9973;
    const vector <ll> mod = {999999937LL, 1000000007LL};
    string S;
    vector <ll> hash[2], pow[2];

public:
    RollingHash(const string &s) {
        S = s;
        int n = S.size();
        for (int i = 0; i < 2; i++) {
            hash[i].assign(n + 1, 0);
            pow[i].assign(n + 1, 1);
            for (int j = 0; j < n; j++) {
                hash[i][j + 1] = (hash[i][j] * base + S[j]) % mod[i];
                pow[i][j + 1] = pow[i][j] * base % mod[i];
            }
        }
    }

    // get hash of S[l:r)
    ll get(int l, int r, int id = 1) {
        ll res = hash[id][r] - hash[id][l] * pow[id][r - l] % mod[id];
        if (res < 0) res += mod[id];
        return res;
    }
};


// verified
//   http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_14_B&lang=jp
void AOJ_ALDS1_14_B() {
    string T, P;
    cin >> T >> P;
    RollingHash rh1(T), rh2(P);
    for (int i = 0; i + P.size() <= T.size(); i++) {
        if (rh1.get(i, i + P.size()) == rh2.get(0, P.size())) {
            cout << i << "\n";
        }
    }
}


// verified
//   https://yukicoder.me/problems/no/430
//   mod = 999999937LL だとハッシュが衝突してWAだった
void yuki430() {
    string S, C;
    int M;
    cin >> S >> M;
    RollingHash rh(S);
    map <ll, int> mp;
    for (int l = 0; l < S.size(); l++) {
        for (int r = 1; r <= 10; r++) {
            if (l + r > S.size()) continue;
            mp[rh.get(l, l + r)]++;
        }
    }
    int ans = 0;
    for (int m = 0; m < M; m++) {
        cin >> C;
        RollingHash rh1(C);
        ans += mp[rh1.get(0, C.size())];
    }
    cout << ans << "\n";
}


// verified
//   https://yukicoder.me/problems/no/599
const ll MOD = 1000000007LL;
vector <ll> dp(100005);
vector <bool> used(100005, false);
ll dfs(RollingHash &rh, int L, int R) {
    if (R <= L) return 1;
    if (used[L]) return dp[L];
    ll ret = 1;
    int l = L + 1, r = R - 1;
    while (l <= r) {
        if (rh.get(L, l) == rh.get(r, R)) ret += dfs(rh, l, r);
        l++; r--;
    }
    used[L] = true;
    return dp[L] = ret;
}
void yuki599() {
    string S;
    cin >> S;
    RollingHash rh(S);
    cout << dfs(rh, 0, S.size()) << "\n";
}


int main() {
    // AOJ_ALDS1_14_B();
    // yuki430();
    yuki599();
    return 0;
}
0