結果
| 問題 |
No.599 回文かい
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-02-21 19:17:30 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 650 ms / 4,000 ms |
| コード長 | 3,384 bytes |
| コンパイル時間 | 2,052 ms |
| コンパイル使用メモリ | 201,020 KB |
| 最終ジャッジ日時 | 2025-01-28 01:16:41 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 22 |
ソースコード
#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;
}