結果

問題 No.263 Common Palindromes Extra
ユーザー sigma425sigma425
提出日時 2017-09-15 04:38:20
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 1,781 bytes
コンパイル時間 2,076 ms
コンパイル使用メモリ 181,020 KB
実行使用メモリ 206,924 KB
最終ジャッジ日時 2024-04-25 10:59:50
合計ジャッジ時間 5,969 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
21,376 KB
testcase_01 AC 10 ms
15,232 KB
testcase_02 AC 12 ms
15,360 KB
testcase_03 AC 54 ms
16,700 KB
testcase_04 AC 217 ms
21,908 KB
testcase_05 AC 170 ms
20,424 KB
testcase_06 AC 33 ms
16,384 KB
testcase_07 MLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
typedef unsigned long long ll;
ll B=1e9+7;
int S[1000000];
ll hs[1000001];
map<ll,ll> has;		//has->num
map<ll,ll> D,E;
map<ll,ll> nxt;
map<ll,vector<ll> > prv;
set<ll> doll;	//$..$
void Manacher(string s){
	int i=0,j=0;
	while(i<s.size()){
		while(i-j>=0&&i+j<s.size()&&s[i-j]==s[i+j]) ++j;
		S[i]=j;
		int k=1;
		while(i-k>=0&&i+k<s.size()&&k+S[i-k]<j) S[i+k]=S[i-k],++k;
		i+=k,j-=k;
	}
}
ll ex(ll x,ll p){
	ll a=1;
	while(p){
		if(p%2) a*=x;
		x*=x;
		p/=2;
	}
	return a;
}
ll H(int x,int y){	//[x,y)
	return hs[y]-hs[x]*ex(B,y-x);
}
void dfs(ll h){
	for(ll x:prv[h]){
		dfs(x);
		has[h]+=has[x];
	}
}
void solve(){
	string s,s_;
	cin>>s_;
	int N=s_.size();
	s=s_[0];
	rep1(i,N-1) s+="$",s+=s_[i];
	N=s.size();
	Manacher(s);
	rep(i,N) hs[i+1]=hs[i]*B+s[i];
	rep(i,N){
		int x=S[i];
		ll h=H(i-x+1,i+x);
		bool fst=1;
		while(!has.count(h)){
			if(fst) has[h]++,fst=0;
			has[h];		//access
			if(s[i-x+1]=='$') doll.insert(h);
			x--;
			if(x==0) break;
			ll nh=H(i-x+1,i+x);
			nxt[h]=nh;
			prv[nh].pb(h);
			h=nh;
		}
		if(fst) has[h]++;
	}
	for(auto p:has){
		ll h=p.fs;
		if(!nxt.count(h)) dfs(h);
	}
}
int main(){
	solve();
	D=has;
	rep(i,1000000) S[i]=0;
	rep(i,1000001) hs[i]=0;
	has.clear();
	nxt.clear();
	prv.clear();
	solve();
	E=has;
	ll ans=0;
/*	for(auto p:E){
		ll h=p.fs;
		show(h);
		show(E[h]);
	}*/
	for(auto p:D){
		ll h=p.fs;
		if(doll.count(h)) continue;
		if(E.count(h)){
			ans+=D[h]*E[h];
		}
	}
	cout<<ans<<endl;
}
0