結果

問題 No.464 PPAP
ユーザー yuppe19 😺yuppe19 😺
提出日時 2016-12-16 16:26:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 611 ms / 2,000 ms
コード長 2,835 bytes
コンパイル時間 870 ms
コンパイル使用メモリ 89,852 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-20 09:00:33
合計ジャッジ時間 3,090 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 27 ms
4,376 KB
testcase_08 AC 8 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 611 ms
4,384 KB
testcase_11 AC 172 ms
4,380 KB
testcase_12 AC 279 ms
4,380 KB
testcase_13 AC 5 ms
4,380 KB
testcase_14 AC 14 ms
4,380 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 3 ms
4,380 KB
testcase_22 AC 3 ms
4,380 KB
testcase_23 AC 4 ms
4,376 KB
testcase_24 AC 5 ms
4,376 KB
testcase_25 AC 5 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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);

  // dp[i] := iから最後までの回文の個数
  vector<i64> dp(n+2, 0); // dp[n+2]
  for(int i=n; i>0; --i) {
    dp[i] = dp[i+1];
    if(rhs.calc_hash(i, n) == rht.calc_hash(1, n+1-i)) { ++dp[i]; }
  }

  i64 res = 0;
  for(int i=1; i<=n; ++i) {
    if(rhs.calc_hash(1, i) != rht.calc_hash(n+1-i, n)) { continue; }
    for(int j=i+1; j+2<=n; ++j) {
      if(rhs.calc_hash(i+1, j) != rht.calc_hash(n+1-j, n-i)) { continue; }
      res += dp[j+2];
    }
  }

  printf("%lld\n", res);
  return 0;
}
0