結果
| 問題 |
No.464 PPAP
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2016-12-16 12:59:18 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,830 bytes |
| コンパイル時間 | 947 ms |
| コンパイル使用メモリ | 91,936 KB |
| 実行使用メモリ | 13,640 KB |
| 最終ジャッジ日時 | 2024-11-30 10:23:56 |
| 合計ジャッジ時間 | 14,952 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 18 TLE * 4 |
ソースコード
#include <iostream>
#include <algorithm>
#include <chrono>
#include <random>
using namespace std;
using i64 = long long;
struct rolling_hash {
rolling_hash(const string& s, const vector<i64>& mods, const vector<i64>& bases);
i64 calc_hash(int l, int r); // [l, r]。つまりlもrも含む。1-indexed
private:
string s;
vector<vector<i64>> b, H; // b[2][s.size()+1], H[2][s.size()+1]
vector<i64> mods, bases;
};
template <class Int>
vector<Int> segment_sieve(Int a, Int b) {
Int sqb = sqrt(b);
vector<bool> is_prime_small(sqb+1, true);
is_prime_small[0] = is_prime_small[1] = false;
vector<bool> is_prime(b-a, true);
is_prime[0] = a != 1;
for(Int i=2; i*i<b; ++i) {
if(is_prime_small[i]) {
for(Int j=2*i; j*j<b; j+=i) { is_prime_small[j] = 0; }
for(Int j=max(Int(2), (a+i-1)/i)*i; j<b; j+=i) {
is_prime[j - a] = false;
}
}
}
vector<Int> res;
for(Int i=0; i<b-a; ++i) {
if(is_prime[i]) { res.push_back(i+a); }
}
return res;
}
rolling_hash::rolling_hash(const string& s, const vector<i64>& mods, const vector<i64>& bases) : s(s), mods(mods), bases(bases) {
b.resize(2);
H.resize(2);
int n = s.size();
i64 B;
for(int j=0; j<2; ++j) {
B = bases[j] % mods[j];
b[j].resize(n+1);
H[j].resize(n+1);
b[j][0] = 1;
H[j][0] = 0;
for(int i=0; i<n; ++i) {
b[j][i+1] = b[j][i] * B;
b[j][i+1] %= mods[j];
H[j][i+1] = H[j][i] * B + s[i];
H[j][i+1] %= mods[j];
}
}
}
i64 rolling_hash::calc_hash(int l, int r) {
i64 h0 = (H[0][r] - b[0][r-l+1] * H[0][l-1]) % mods[0];
if(h0 < 0) { h0 += mods[0]; }
i64 h1 = (H[1][r] - b[1][r-l+1] * H[1][l-1]) % mods[1];
if(h1 < 0) { h1 += mods[1]; }
i64 hash = (h0 << 31) + h1;
return hash;
}
int main(void) {
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
mt19937_64 genrand(seed);
constexpr i64 LO = i64(powl(2, 30)),
HI = LO + 100000;
vector<i64> primes = segment_sieve(LO, HI);
shuffle(begin(primes), end(primes), genrand);
vector<i64> mods, bases;
for(int i=0; i<2; ++i) {
mods.push_back(primes[i]);
bases.push_back(genrand());
}
string s, t;
cin >> s;
int n = s.size();
t = s;
reverse(begin(t), end(t));
rolling_hash rhs(s, mods, bases),
rht(t, mods, bases);
int cnt = 0;
int i1 = 1;
for(int j1=i1; j1<=n; ++j1) { // P1
if(rhs.calc_hash(i1, j1) != rht.calc_hash(n+1-j1, n+1-i1)) { continue; }
int i2 = j1 + 1;
for(int j2=i2; j2<=n; ++j2) { // P2
if(rhs.calc_hash(i2, j2) != rht.calc_hash(n+1-j2, n+1-i2)) { continue; }
for(int i3=j2+2; i3<=n; ++i3) { // +2
int j3 = n;
if(rhs.calc_hash(i3, j3) != rht.calc_hash(n+1-j3, n+1-i3)) { continue; }
++cnt;
}
}
}
printf("%d\n", cnt);
return 0;
}