結果

問題 No.599 回文かい
ユーザー utouto97utouto97
提出日時 2019-08-15 10:04:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,279 ms / 4,000 ms
コード長 2,284 bytes
コンパイル時間 1,742 ms
コンパイル使用メモリ 169,388 KB
実行使用メモリ 4,520 KB
最終ジャッジ日時 2023-10-19 18:42:33
合計ジャッジ時間 6,708 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define int long long
#define rep(i,l,r) for(int i=(int)(l);i<(int)(r);i++)
#define all(x) (x).begin(),(x).end()
template<class T>bool chmax(T &a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a,T b){if(a>b){a=b;return 1;}return 0;}

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

const int inf = 1LL<<60;
const int mod = 1e9 + 7;
const double eps = 1e-9;

/*{
  }*/

template<typename T>
class RollingHash{
  public:
    const int K = 2;
    int N;
    vector<T> M, B;
    vector<vector<T>> hash, p;

    RollingHash(){}

    RollingHash(const string &s, vector<T> b={107,311}){
      init(s, b);
    }

    void init(const string &s,
        vector<T> b={107, 311},
        vector<T> m={1000000007, 1000000009}){
      
      M = m; B = b;
      hash.resize(K); p.resize(K);

      N = s.size();
      for(int i = 0; i < K; i++){
        hash[i].assign(N+1, 0); 
        p[i].assign(N+1, 1);

        for(int j = 0; j < N; j++){
          hash[i][j+1] = (hash[i][j]*B[i] + s[j])%M[i];
          p[i][j+1] = p[i][j]*B[i]%M[i];
        }
      }
    }

    void add(const string &s){
      int m = s.size();
      for(int i = 0; i < K; i++){
        hash[i].resize(N+m+1, 0);
        p[i].resize(N+m+1, 1);

        for(int j = 0; j < m; j++){
          hash[i][N+j+1] = (hash[i][N+j]*B[i] + s[j])%M[i];
          p[i][N+j+1] = p[i][N+j]*B[i]%M[i];
        }
      }
      N += m;
    }

    vector<T> find(int l, int r){
      vector<T> res(K);
      for(int i = 0; i < K; i++){
        T tmp = hash[i][r] + M[i] - hash[i][l]*p[i][r-l]%M[i];
        res[i] = tmp >= M[i] ? tmp-M[i] : tmp;
      }
      return res;
    }
};

int memo[10000];
bool vis[10000];
RollingHash<int> rh;

int dfs(int L, int R){
  if(R < L) return 1;
  if(vis[L]) return memo[L];

  int res = 1;
  int l = L, r = R;
  while(l < r){
    if(rh.find(L, l+1) == rh.find(r, R+1)){
      (res += dfs(l+1, r-1)) %= mod;
    }
    l++; r--;
  }

  vis[L] = true;
  return memo[L] = res;
}

signed main(){
  srand((unsigned)time(NULL));

  int B1 = rand()%10000+100000;
  int B2 = rand()%10000+500000;

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

  rh.init(s, {B1, B2});
  cout << dfs(0, n-1) << endl;

  return 0;
}
0