結果

問題 No.263 Common Palindromes Extra
ユーザー latte0119latte0119
提出日時 2017-06-11 21:11:04
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 171 ms / 2,000 ms
コード長 1,854 bytes
コンパイル時間 1,323 ms
コンパイル使用メモリ 157,228 KB
実行使用メモリ 163,000 KB
最終ジャッジ日時 2023-08-20 00:36:35
合計ジャッジ時間 2,881 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
11,504 KB
testcase_01 AC 2 ms
5,432 KB
testcase_02 AC 2 ms
5,408 KB
testcase_03 AC 16 ms
21,724 KB
testcase_04 AC 65 ms
85,140 KB
testcase_05 AC 93 ms
87,080 KB
testcase_06 AC 10 ms
12,780 KB
testcase_07 AC 98 ms
106,056 KB
testcase_08 AC 116 ms
105,740 KB
testcase_09 AC 122 ms
125,656 KB
testcase_10 AC 171 ms
163,000 KB
testcase_11 AC 60 ms
87,068 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

struct PalindromicTree {
	struct node {
		map<char,int> next;
		int len, sufflink,num,first;
	};
	string S;
	vector<node> V;
	int n,s; // num,suff

	bool add(int pos) {
		char ch=S[pos];
		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.count(ch)) {
			s = V[c].next[ch];
			return false;
		}

		s=++n;
		V[n].len=V[c].len+2;
		V[n].first=pos;
		V[c].next[ch]=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[ch];
				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;
	}
};

int dps[1111111],dpt[1111111];

signed main(){
    cin.tie(0);ios_base::sync_with_stdio(0);

    string S,T;
    cin>>S>>T;

    string ST=S+"@*"+T;
    PalindromicTree pt;pt.init(ST);
    for(int i=0;i<ST.size();i++){
        pt.add(i);
        if(i<S.size())dps[pt.s]++;
        else if(i>S.size())dpt[pt.s]++;
    }

    int ans=0;
    for(int i=pt.n;i>2;i--){
        ans+=dps[i]*dpt[i];
        dps[pt.V[i].sufflink]+=dps[i];
        dpt[pt.V[i].sufflink]+=dpt[i];
    }

    cout<<ans<<endl;
    return 0;
}
0