結果

問題 No.599 回文かい
ユーザー koyumeishikoyumeishi
提出日時 2016-06-03 20:14:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,912 ms / 4,000 ms
コード長 1,892 bytes
コンパイル時間 1,175 ms
コンパイル使用メモリ 94,252 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 23:36:45
合計ジャッジ時間 7,945 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <cassert>
#include <random>
#include <chrono>
using namespace std;

class RollingHash{
  const string& str_;
  vector<long long> c_;
  vector<long long> mod_;
  vector<vector<long long>> hash_;
  vector<vector<long long>> pow_mod_;
 public:
  RollingHash(const string& s, const vector<long long>& c, const vector<long long>& mod)
   : str_(s),
     c_(c.begin(), c.end()), 
     mod_(mod.begin(), mod.end()), 
     hash_(mod.size(), vector<long long>(s.size()+1, 0)), 
     pow_mod_(mod.size(), vector<long long>(s.size()+1, 1))
  {
    assert(c_.size() > 0); assert(c_.size() == mod_.size());
    for(int k=0; k<mod_.size(); ++k) for(int i=1; i<=str_.size(); ++i){
      pow_mod_[k][i] = pow_mod_[k][i-1] * c_[k] % mod_[k];
    }
    for(int k=0; k<mod_.size(); ++k) for(int i=1; i<=str_.size(); ++i){
      hash_[k][i] = (hash_[k][i-1] * c_[k] + str_[i-1]) % mod_[k];
    }
  }

  // [l,r)
  vector<long long> get_hash(int l, int r){
    vector<long long> ret(mod_.size());
    int len = r-l;
    for(int k=0; k<mod_.size(); ++k){
      ret[k] = (hash_[k][r] - hash_[k][l] * pow_mod_[k][len] % mod_[k] + mod_[k]) % mod_[k];
    }
    return ret;
  }
};

const long long mod = 1000000007;

int main(){
  string s;
  cin >> s;
  int n = s.size();

  mt19937 mt(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count());
  RollingHash rh(s, {100009ll, vector<long long>{114,514,893,1919}[mt()%4]}, {1000000007ll, 1000000009ll});

  vector<long long> dp(n+1, 0);
  dp[0] = 1;

  long long ans = 1;
  for(int i=1; i<=n/2; i++){
    for(int j=0; j<i; j++){
      if(dp[j]==0) continue;
      auto x = rh.get_hash(j,i);
      auto y = rh.get_hash(n-i, n-j);
      if(x==y){
        (dp[i] += dp[j]) %= mod;
      }
    }
    (ans += dp[i]) %= mod;
  }

  printf("%lld", ans);
  return 0;
}
0