結果

問題 No.599 回文かい
ユーザー koyumeishikoyumeishi
提出日時 2016-06-03 20:37:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,783 ms / 4,000 ms
コード長 3,804 bytes
コンパイル時間 1,337 ms
コンパイル使用メモリ 110,004 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-19 08:08:22
合計ジャッジ時間 10,880 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 3 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 13 ms
6,940 KB
testcase_11 AC 11 ms
6,940 KB
testcase_12 AC 25 ms
6,944 KB
testcase_13 AC 34 ms
6,944 KB
testcase_14 AC 2,317 ms
6,940 KB
testcase_15 AC 150 ms
6,940 KB
testcase_16 AC 2,403 ms
6,944 KB
testcase_17 AC 2,783 ms
6,940 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,944 KB
evil_0.txt AC 1,052 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

//Manber & Myers
//O(n (log n)^2)
class SuffixArray{
 public:
  vector<int> sa;
  vector<int> lcp;
  vector<int> rank;

  SuffixArray(const string& SS)
   : S(SS),
     len(SS.size()),
     sa(SS.size()+1),
     lcp(SS.size()+1),
     rank(SS.size()+1)
  {
    constract_sa();
    constract_lcp();
  }

  void dbg_print(){
    for(int i=0; i<=len; i++){
      cerr << S.substr(sa[i]) << " " << lcp[i] << endl;
    }
  }

 private:
  const string& S;
  int len;

  //Manber & Myers
  //O(len (logN)^2)
  void constract_sa(){
    //first character
    for(int i=0; i<=len; i++){
      sa[i] = i;
      rank[i] = (i<len) ? S[i] : -1;
    }

    vector<int> tmp(len+1);
    for(int k=1; k<=len; k *= 2){
      auto cmp_func = [&](int x, int y) -> bool{
        if(rank[x] != rank[y]) return rank[x] < rank[y];
        int l = (x+k <= len) ? rank[x+k] : -1;
        int r = (y+k <= len) ? rank[y+k] : -1;
        return l < r;
      };
      sort(sa.begin(), sa.end(), cmp_func);
      tmp[sa[0]] = 0;
      for(int i=1; i<=len; i++){
        tmp[sa[i]] = tmp[sa[i-1]] + (cmp_func(sa[i-1],sa[i])==true?1:0);
      }
      for(int i=0; i<=len; i++){
        rank[i] = tmp[i];
      }
    }
  }

  void constract_lcp(){
    for(int i=0; i<=len; i++) rank[sa[i]] = i;
    int h=0;
    lcp[0] = 0;
    for(int i=0; i<len; i++){
      int j=sa[rank[i]-1];
      if(h>0) h--;
      for(; j+h<len && i+h<len; h++){
        if(S[j+h] != S[i+h]) break;
      }
      lcp[rank[i] - 1] = h;
    }
  }
};

template<class Value = int>
class SegmentTree{
  int n;
  vector<Value> V;
  Value DEFAULT_VALUE;

  //evaluation function
  static Value default_evaluate(Value a, Value b){
    return max(a,b);
  }

  function< Value(Value, Value) > evaluate;

  //return evaluated value in [a,b)
  //T[at] covers [l,r)
  Value RangeEvaluation(int a, int b, int at, int l, int r){
    //out of range
    if(r <= a || b <= l) return DEFAULT_VALUE;
    //covered
    if(a <= l && r <= b) return V[at];

    //partially covered
    else{
      Value val_left = RangeEvaluation(a,b, at*2+1, l, (l+r)/2);
      Value val_right = RangeEvaluation(a,b, at*2+2, (l+r)/2, r);
      return evaluate(val_left, val_right);
    }
  }

 public:
  SegmentTree(int size, Value DEFAULT = 0, function< Value(Value, Value) > eval = default_evaluate){
    DEFAULT_VALUE = DEFAULT;
    evaluate = eval;
    n=1;
    while(n<size) n <<= 1;
    V = vector<Value>(2*n - 1, DEFAULT_VALUE);
  }

  void update(int at, Value new_val){
    at += n-1;
    V[at] = new_val;
    while(at>0){
      at = (at-1)/2;
      V[at] = evaluate(V[at*2 + 1], V[at*2 + 2]);
    }
  }


  //return evaluated value in [l,r)
  Value RangeEvaluation(int l, int r){
    if(l>=r) return DEFAULT_VALUE;
    if(l>=n) return DEFAULT_VALUE;
    return RangeEvaluation(l,r, 0, 0, n);
  }
};


const long long mod = 1000000007;
int main(){
  string s;
  cin >> s;
  int n = s.size();
  assert(1<=n && n<=10000);
  for(char c : s) assert('a'<=c && c<='z');

  SuffixArray sa(s);
  SegmentTree<int> sg(n+1, 114514, [](int a, int b){return min(a,b);});
  for(int i=0; i<sa.lcp.size(); i++){
    sg.update(i, sa.lcp[i]);
  }

  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]) continue;

      int ra = sa.rank[j];
      int rb = sa.rank[n-i];
      if(ra>rb) swap(ra,rb);
      int len = i-j;
      int lcp = sg.RangeEvaluation(ra,rb);

      if(lcp>=len){
        dp[i] += dp[j];
        if(dp[i] >= mod) dp[i] -= mod;
      }
    }
    ans += dp[i];
    if(ans>=mod) ans -= mod;
  }

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