結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
11,476 KB
testcase_01 AC 2 ms
5,396 KB
testcase_02 AC 2 ms
5,452 KB
testcase_03 AC 16 ms
21,528 KB
testcase_04 AC 65 ms
86,576 KB
testcase_05 AC 95 ms
86,364 KB
testcase_06 AC 8 ms
12,744 KB
testcase_07 AC 95 ms
106,040 KB
testcase_08 AC 115 ms
105,856 KB
testcase_09 AC 119 ms
125,456 KB
testcase_10 AC 174 ms
162,996 KB
testcase_11 AC 58 ms
86,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>nex;
        int len,suflink,height,firapp;
        //length,suffix link,height,first appearance
	};

	string s;
	vector<node>v;
    int n,suf;

	bool add(int pos){
		char ch=s[pos];
		int cur=suf;
		while(true){
			if(pos-1-v[cur].len>=0&&s[pos-1-v[cur].len]==ch)break;
			cur=v[cur].suflink;
		}

		if(v[cur].nex.count(ch)){
			suf=v[cur].nex[ch];
			return false;
		}

		suf=n++;

		v[suf].len=v[cur].len+2;
		v[suf].firapp=pos;
		v[cur].nex[ch]=suf;

		if(v[suf].len==1){ // even length
			v[suf].suflink=1;
			v[suf].height=1;
			return true;
		}

		while(true){
			cur=v[cur].suflink;
			if(pos-1-v[cur].len>=0&&s[pos-1-v[cur].len]==ch){
				v[suf].suflink=v[cur].nex[ch];
				break;
			}
		}
		v[suf].height=1+v[v[suf].suflink].height;
		return true;
	}

	void init(const string &s){
		this->s=s;
		v.clear();
		v.resize(s.size()+10);

		n=2;
		suf=1;
		v[0].firapp=v[1].firapp=-1;
		v[0].len=-1;
		v[1].len=0;
		v[0].suflink=v[1].suflink=0;
		v[0].height=v[1].height=0;
	}
};

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.suf]++;
        else if(i>S.size())dpt[pt.suf]++;

    }


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

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