結果

問題 No.263 Common Palindromes Extra
ユーザー beetbeet
提出日時 2019-10-15 18:09:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 275 ms / 2,000 ms
コード長 2,971 bytes
コンパイル時間 5,888 ms
コンパイル使用メモリ 220,456 KB
実行使用メモリ 187,788 KB
最終ジャッジ日時 2023-08-28 17:21:24
合計ジャッジ時間 5,540 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 23 ms
32,660 KB
testcase_01 AC 11 ms
27,000 KB
testcase_02 AC 12 ms
26,956 KB
testcase_03 AC 35 ms
42,672 KB
testcase_04 AC 111 ms
106,424 KB
testcase_05 AC 133 ms
106,004 KB
testcase_06 AC 23 ms
33,940 KB
testcase_07 AC 205 ms
147,044 KB
testcase_08 AC 230 ms
146,708 KB
testcase_09 AC 273 ms
187,788 KB
testcase_10 AC 275 ms
187,768 KB
testcase_11 AC 103 ms
105,712 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
#endif
//BEGIN CUT HERE
struct PalindromicTree{
  struct node{
    map<char, int> nxt;
    int len,suf,app,cnt;
    node(){}
    node(int len,int suf,int app,int cnt)
      :len(len),suf(suf),app(app),cnt(cnt){}
  };
  vector<node> v;
  vector<int> ord;
  int n,ptr;

  PalindromicTree(){}
  PalindromicTree(const string &s)
    :v(s.size()+10),n(2),ptr(1){

    v[0]=node(-1,0,-1,0);
    v[1]=node( 0,0,-1,0);

    for(int i=0;i<(int)s.size();i++) add_char(s,i);
    calc_order();
    calc_count();
  }

  bool add_char(const string &s,int pos){
    char ch=s[pos];
    int cur=ptr;
    while(1){
      int rev=pos-1-v[cur].len;
      if(rev>=0&&s[rev]==ch) break;
      cur=v[cur].suf;
    }

    if(v[cur].nxt.count(ch)){
      ptr=v[cur].nxt[ch];
      v[ptr].cnt++;
      return false;
    }
    ptr=n++;

    v[ptr]=node(v[cur].len+2,-1,pos-v[cur].len-1,1);
    v[cur].nxt[ch]=ptr;

    if(v[ptr].len==1){
      v[ptr].suf=1;
      return true;
    }

    while(1){
      cur=v[cur].suf;
      int rev=pos-1-v[cur].len;
      if(rev>=0&&s[rev]==ch){
        v[ptr].suf=v[cur].nxt[ch];
        break;
      }
    }
    return true;
  }

  void calc_order(){
    ord.clear();
    ord.push_back(0);
    ord.push_back(1);
    for(int i=0;i<(int)ord.size();i++)
      for(auto &p:v[ord[i]].nxt) ord.push_back(p.second);
  }

  void calc_count(){
    for(int i=(int)ord.size()-1;i>=0;i--)
      v[v[ord[i]].suf].cnt+=v[ord[i]].cnt;
  }
};
//END CUT HERE
#ifndef call_from_test

template<typename T,T MOD,T B>
struct RollingHash{
  using ll = long long;
  vector<T> hash,p;
  RollingHash(){}
  RollingHash(const string &s){
    int n=s.size();
    hash.assign(n+1,0);
    p.assign(n+1,1);
    for(int i=0;i<n;i++){
      hash[i+1]=((ll)hash[i]*B+s[i])%MOD;
      p[i+1]=(ll)p[i]*B%MOD;
    }
  }
  //S[l, r)
  T find(int l,int r){
    T res=(ll)hash[r]+MOD-(ll)hash[l]*p[r-l]%MOD;
    return res>=MOD?res-MOD:res;
  }
};

//INSERT ABOVE HERE
signed main(){
  using ll = long long;
  string s,t;
  cin>>s>>t;

  PalindromicTree p1(s),p2(t);
  const int MOD = 1e9+7;
  const int BASE1 = 1777771;
  const int BASE2 = 1e6+3;
  RollingHash<int, MOD, BASE1> rs1(s),rt1(t);
  RollingHash<int, MOD, BASE2> rs2(s),rt2(t);

  const int MAX = 5e5+100;
  map<pair<int, int>, int> m1[MAX];
  for(int i=0;i<(int)p1.n;i++){
    PalindromicTree::node& u=p1.v[i];
    if(u.app<0) continue;
    auto p=make_pair(rs1.find(u.app,u.app+u.len),
                     rs2.find(u.app,u.app+u.len));
    m1[u.len][p]=u.cnt;
  }

  ll ans=0;
  for(int i=0;i<(int)p2.n;i++){
    PalindromicTree::node& u=p2.v[i];
    auto p=make_pair(rt1.find(u.app,u.app+u.len),
                     rt2.find(u.app,u.app+u.len));
    if(u.app<0||!m1[u.len].count(p)) continue;
    ans+=(ll)m1[u.len][p]*u.cnt;
  }

  cout<<ans<<endl;
  return 0;
}
/*
  verified on 2019/10/15
  https://yukicoder.me/problems/no/263
*/
#endif
0