結果

問題 No.263 Common Palindromes Extra
ユーザー 👑 kmjpkmjp
提出日時 2015-08-09 10:46:36
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 132 ms / 2,000 ms
コード長 2,119 bytes
コンパイル時間 1,329 ms
コンパイル使用メモリ 153,308 KB
実行使用メモリ 147,784 KB
最終ジャッジ日時 2023-08-20 00:29:49
合計ジャッジ時間 2,968 ms
ジャッジサーバーID
(参考情報)
judge14 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
14,576 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 18 ms
32,120 KB
testcase_04 AC 99 ms
147,620 KB
testcase_05 AC 95 ms
147,784 KB
testcase_06 AC 9 ms
16,484 KB
testcase_07 AC 104 ms
147,596 KB
testcase_08 AC 100 ms
147,640 KB
testcase_09 AC 125 ms
147,692 KB
testcase_10 AC 132 ms
147,508 KB
testcase_11 AC 92 ms
147,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<to;x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

// nはノード数、sは現在位置
// firstは回文の末尾


struct PalindromicTree {
	struct node {
		int next[28];
		int len, sufflink,num,first;
	};
	string S;
	vector<node> V;
	int n,s; // num,suff
	
	bool add(int pos) {
		char ch=S[pos];
		int nch=ch-'a'; // lower
		int c=s, cl=0;
		while(1) {
			cl=V[c].len;
			if(pos-1-cl>=0 && S[pos-1-cl]==ch) break;
			c=V[c].sufflink;
		}
		if(V[c].next[nch]) {
			s = V[c].next[nch];
			return false;
		}
		
		s=++n;
		V[n].len=V[c].len+2;
		V[n].first=pos;
		V[c].next[nch]=n;
		
		if(V[n].len==1) { // even length
			V[n].sufflink=2;
			V[n].num=1;
			return true;
		}
		
		while(1) {
			c=V[c].sufflink;
			cl=V[c].len;
			if(pos-1-cl>=0 && S[pos-1-cl]==ch) {
				V[n].sufflink=V[c].next[nch];
				break;
			}
		}
		V[n].num=1+V[V[n].sufflink].num;
		return true;
	}
	
	void init(string S) {
		this->S=S;
		V.clear();
		V.resize(S.size()+10);
		
		n=s=2;
		V[1].first=V[2].first=-1;
		V[1].len=-1;
		V[2].len=0;
		V[1].sufflink=V[2].sufflink=1;
	}
};


string S,T,V;
vector<ll> dp[2];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>S>>T;
	V=S+"{|"+T;
	dp[0].resize(V.size()+5);
	dp[1].resize(V.size()+5);
	
	
	PalindromicTree pt;
	pt.init(V);
	
	
	FOR(i,V.size()) {
		pt.add(i);
		if(i<S.size()) dp[0][pt.s]++;
		if(i>=S.size()+1) dp[1][pt.s]++;
	}
	ll ret=0;
	for(i=pt.n;i>=3;i--) {
		ret += dp[0][i]*dp[1][i];
		dp[0][pt.V[i].sufflink]+=dp[0][i];
		dp[1][pt.V[i].sufflink]+=dp[1][i];
	}
	cout<<ret<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}
0