結果

問題 No.599 回文かい
ユーザー たこしたこし
提出日時 2017-11-24 22:35:26
言語 C++11
(gcc 13.3.0)
結果
MLE  
実行時間 -
コード長 954 bytes
コンパイル時間 1,117 ms
コンパイル使用メモリ 158,460 KB
実行使用メモリ 786,048 KB
最終ジャッジ日時 2024-11-27 07:26:48
合計ジャッジ時間 12,530 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other MLE * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define INF 1145141919
#define INF_LL 114514191981036
using LL = long long int;
using ULL = unsigned long long int;
#define II pair<int, int>

using namespace std;

string S;
const LL mod = 1000000007;
const int MAX_S = 10005;

LL dp[MAX_S][MAX_S];

ULL Hash[MAX_S];
ULL BaseB[MAX_S];
ULL Base = 1000000000000009;

void reg()
{
  BaseB[0] = 1;
  for (size_t i = 0; i < S.length(); i++) {
    Hash[i+1] = Hash[i] * Base;
    Hash[i+1] += S[i];
    BaseB[i+1] = BaseB[i] * Base;
  }
}

ULL calc(int l, int r)
{
  return Hash[r] - Hash[l] * BaseB[r-l];
}

LL solve(int l, int r)
{
  if(dp[l][r] != -1) {
    return dp[l][r];
  }

  LL ret = 1;

  for (size_t i = 1; l+i < r-i; i++) {
    if(calc(l, l+i) == calc(r-i, r)) {
      ret += solve(l+i, r-i);
      ret %= mod;
    }
  }

  return dp[l][r] = ret;
}

int main()
{
  cin >> S;
  memset(dp, -1, sizeof(dp));

  reg();

  cout << solve(0, S.length()) << endl;

  return 0;
}
0