結果

問題 No.263 Common Palindromes Extra
ユーザー 沙耶花沙耶花
提出日時 2022-09-28 10:31:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,950 ms / 2,000 ms
コード長 3,852 bytes
コンパイル時間 4,540 ms
コンパイル使用メモリ 276,888 KB
実行使用メモリ 92,472 KB
最終ジャッジ日時 2023-08-24 04:19:11
合計ジャッジ時間 13,058 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
7,120 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 118 ms
13,932 KB
testcase_04 AC 669 ms
54,860 KB
testcase_05 AC 542 ms
57,640 KB
testcase_06 AC 29 ms
7,644 KB
testcase_07 AC 888 ms
77,812 KB
testcase_08 AC 734 ms
77,248 KB
testcase_09 AC 1,950 ms
92,472 KB
testcase_10 AC 1,935 ms
92,416 KB
testcase_11 AC 383 ms
55,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
using mint = modint998244353;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 4000000000000000001

struct rolling_hash{

	long long t_hash;
	static vector<long long> power;
	static const long long MOD = (1LL<<61)-1;
	static const long long b = 123456;
	int sz;
	
	rolling_hash(){		
		sz = 0;
		t_hash = 0;
	}
	
	rolling_hash(char c){
		sz = 1;
		t_hash = b*c;
	}

	long long mul(__int128 x,__int128 y){
		__int128 t = x*y;
		t = (t>>61) + (t&MOD);
		
		if(t>=MOD)t -= MOD;
		return t;
	}
	
	long long get_pow(int sz){
		if(power.size()>sz)return power[sz];
		
		while(power.size()<=sz){
			if(power.size()==0)power.push_back(1);
			else power.push_back(mul(power.back(),b));
		}
		return power.back();
		
	}
	
	rolling_hash &operator+=(const rolling_hash &another){
		
		(*this).t_hash = mul((*this).t_hash,get_pow(another.sz));
		(*this).t_hash += another.t_hash;
		if((*this).t_hash>=MOD)(*this).t_hash -= MOD;
			
		(*this).sz += another.sz;
		
		return (*this);
	}
	
	rolling_hash operator+(const rolling_hash &another)const{
		return (rolling_hash(*this)+=another);
	}
	
	rolling_hash &operator-=(const rolling_hash &another){

		(*this).t_hash += MOD - mul(another.t_hash,get_pow((*this).sz-another.sz));
		if((*this).t_hash>=MOD)(*this).t_hash -= MOD;
			
		(*this).sz -= another.sz;

		return (*this);
	}
	
	rolling_hash operator-(const rolling_hash &another)const{
		return (rolling_hash(*this)-=another);
	}
	
	bool operator<(const rolling_hash &another)const{
		if((*this).t_hash!=another.t_hash)return (*this).t_hash<another.t_hash;
		return (*this).sz<another.sz;
	}
	
	bool operator==(const rolling_hash &another)const{
		return ((*this).t_hash==another.t_hash && (*this).sz==another.sz);
	}

	
};

vector<long long> rolling_hash::power;

bool check(vector<rolling_hash> &R,vector<rolling_hash> &rR,int l,int r){
	int n = R.size()-1;
	return (R[r]-R[l]) == (rR[n-l]-rR[n-r]);
}
	
int op(int a,int b){
	return min(a,b);
}

int e(){
	return Inf32;
}

int len;
bool f(int x){
	return x >= len;
}

int main(){
	
	string s,t;
	cin>>s>>t;
	
	int n = s.size();
	
	vector<rolling_hash> R(n+1),rR(n+1);
	rep(i,n){
		R[i+1] = R[i] + rolling_hash(s[i]);
	}
	reverse(s.begin(),s.end());
	rep(i,n){
		rR[i+1] = rR[i] + rolling_hash(s[i]);
	}
	reverse(s.begin(),s.end());
	
	set<rolling_hash> S;
	vector<int> l,r;
	rep(i,n){
		int ok = 0,ng = min(i,n-1-i) + 1;
		while(ng-ok>1){
			int mid = (ok+ng)/2;
			if(check(R,rR,i-mid,i+mid+1))ok = mid;
			else ng = mid;
		}
		while(ok>=0){
			auto tr = R[i+ok+1] - R[i-ok];
			if(S.count(tr))break;
			S.insert(tr);
			l.push_back(i-ok);
			r.push_back(i+ok+1);
			ok--;
		}
	}
	rep(i,n-1){
		if(s[i]!=s[i+1])continue;
		int ok = 0,ng = min(i,n-1-(i+1)) + 1;
		while(ng - ok > 1){
			int mid = (ok+ng)/2;
			if(check(R,rR,i-mid,i+1+mid+1))ok = mid;
			else ng = mid;
		}
		while(ok>=0){
			auto tr = R[i+1+ok+1] - R[i-ok];
			if(S.count(tr))break;
			S.insert(tr);
			l.push_back(i-ok);
			r.push_back(i+1+ok+1);
			ok--;
		}
	}
	
	string st = s + " " + t;
	auto sa = suffix_array(st);
	auto la = lcp_array(st,sa);
	
	vector<int> pos(s.size());
	rep(i,sa.size()){
		if(sa[i]<n)pos[sa[i]] = i;
	}
	
	segtree<int,op,e> seg(la);
	rep(i,l.size()){
		int pp = pos[l[i]];
		len = r[i] - l[i];
		int L = seg.min_left<f>(pp),R = seg.max_right<f>(pp);
		R++;
		l[i] = L;
		r[i] = R;
	}
	fenwick_tree<long long> F(sa.size());
	vector<long long> xy(l.size(),0);
	rep(i,sa.size()){
		if(sa[i]<n)F.add(i,1LL);
	}
	rep(i,l.size()){
		xy[i] = F.sum(l[i],r[i]);
	}
	rep(i,sa.size()){
		if(sa[i]<n)F.add(i,-1LL);
		else F.add(i,1LL);
	}
	rep(i,l.size()){
		xy[i] *= F.sum(l[i],r[i]);
	}
	
	long long ans = 0;
	rep(i,xy.size())ans += xy[i];
	
	cout<<ans<<endl;
	
	return 0;
}
0