結果

問題 No.263 Common Palindromes Extra
ユーザー nok0nok0
提出日時 2022-11-30 11:46:18
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,539 bytes
コンパイル時間 2,741 ms
コンパイル使用メモリ 221,748 KB
実行使用メモリ 130,712 KB
最終ジャッジ日時 2024-04-16 13:43:45
合計ジャッジ時間 10,999 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
12,152 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 3 ms
5,376 KB
testcase_03 AC 43 ms
11,100 KB
testcase_04 AC 206 ms
40,448 KB
testcase_05 AC 167 ms
39,784 KB
testcase_06 AC 21 ms
9,152 KB
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, x)    for(int i = 0; i < (x); i++)
#define rng(i, l, r) for(int i = (l); i < (r); i++)
using vi = vector<int>;
#define sz(x) (int)(x).size()

array<vi, 2> manacher(string &s) {
	int n = sz(s);
	array<vi, 2> p = {vi(n + 1), vi(n)};
	rep(z, 2) for(int i = 0, l = 0, r = 0; i < n; i++) {
		int t = r - i + !z;
		if(i < r) p[z][i] = min(t, p[z][l + t]);
		int L = i - p[z][i], R = i + p[z][i] - !z;
		while(L >= 1 and R + 1 < n and s[L - 1] == s[R + 1])
			p[z][i]++, L--, R++;
		if(R > r) l = L, r = R;
	}
	return p;
}


typedef uint64_t ull;
struct H {
	ull x;
	H(ull x = 0) : x(x) {}
	H operator+(H o) { return x + o.x + (x + o.x < x); }
	H operator-(H o) { return *this + ~o.x; }
	H operator*(H o) {
		auto m = (__uint128_t)x * o.x;
		return H((ull)m) + (ull)(m >> 64);
	}
	ull get() const { return x + !~x; }
	bool operator==(H o) const { return get() == o.get(); }
	bool operator<(H o) const { return get() < o.get(); }
};

static const H C = (ll)1e11 + 3;

struct hash_interval {
	vector<H> ha, pw;
	hash_interval(string &str) : ha(sz(str) + 1), pw(ha) {
		pw[0] = 1;
		rep(i, sz(str))
		    ha[i + 1] = ha[i] * C + str[i],
		           pw[i + 1] = pw[i] * C;
	}
	H get(int a, int b) { return ha[b] - ha[a] * pw[b - a]; }
};

map<ull, ll> palindromes_freq(string &s) {
	auto sm = manacher(s);
	hash_interval sh(s);
	set<ull> st;
	map<ull, ull> es;
	vector<set<ull>> nodes(sz(s) + 1);
	map<ull, ll> cum;
	rep(i, sz(s) + 1) {
		if(sm[0][i]) {
			int len = sm[0][i];
			auto ha = sh.get(i - len, i + len);
			cum[ha.get()]++;
			nodes[len * 2].insert(ha.get());
			while(len > 1) {
				len--;
				auto nx = sh.get(i - len, i + len);
				if(es.count({ha.get()})) break;
				es[ha.get()] = nx.get();
				ha = nx;
				nodes[len * 2].insert(ha.get());
			}
		}
	}

	rep(i, sz(s)) {
		sm[1][i]++;
		int len = sm[1][i];
		auto ha = sh.get(i - len + 1, i + len);
		cum[ha.get()]++;
		nodes[len * 2 - 1].insert(ha.get());
		while(len > 1) {
			len--;
			auto nx = sh.get(i - len + 1, i + len);
			if(es.count({ha.get()})) break;
			es[ha.get()] = nx.get();
			ha = nx;
			nodes[len * 2 - 1].insert(ha.get());
		}
	}

	for(int i = sz(s); i >= 0; --i) {
		for(auto &v : nodes[i]) {
			if(es.count(v)) {
				cum[es[v]] += cum[v];
			}
		}
	}
	return cum;
}

int main() {
	string s, t;
	cin >> s >> t;
	auto s_freq = palindromes_freq(s);
	auto t_freq = palindromes_freq(t);
	ll res = 0;
	for(auto [key, cnt] : s_freq) res += cnt * t_freq[key];
	cout << res << endl;
}
0