結果

問題 No.263 Common Palindromes Extra
ユーザー Yang33Yang33
提出日時 2018-08-31 10:30:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 163 ms / 2,000 ms
コード長 3,241 bytes
コンパイル時間 3,590 ms
コンパイル使用メモリ 178,284 KB
実行使用メモリ 126,768 KB
最終ジャッジ日時 2023-08-20 00:37:04
合計ジャッジ時間 3,778 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 12 ms
4,380 KB
testcase_04 AC 58 ms
6,556 KB
testcase_05 AC 73 ms
6,712 KB
testcase_06 AC 7 ms
4,376 KB
testcase_07 AC 87 ms
37,588 KB
testcase_08 AC 99 ms
37,188 KB
testcase_09 AC 106 ms
69,176 KB
testcase_10 AC 163 ms
126,768 KB
testcase_11 AC 43 ms
6,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2018/08/31  Problem: yukicoder263 / Link: https://yukicoder.me/problems/no/263  ----- */
/* ------問題------

-----問題ここまで----- */
/* -----解説等-----

すごい!EERTREE!いごす

----解説ここまで---- */

struct PalindromicTree {
	struct node {
		map<char, int> link;
		int suffix_link, len, count;
	};

	vector<node> c;
	string s;
	int active_idx;

	node* create_node() {
		c.emplace_back();
		node* ret = &c.back();
		ret->count = 0;
		return ret;
	}

	int find_prev_palindrome_idx(int node_id) {
		const int pos = int(s.size()) - 1;
		while (true) {
			const int opposite_side_idx = pos - 1 - c[node_id].len;
			if (opposite_side_idx >= 0 && s[opposite_side_idx] == s.back()) break;
			node_id = c[node_id].suffix_link;
		}
		return node_id;
	}

public:
	PalindromicTree() {
		node* size_m1 = create_node();
		size_m1->suffix_link = 0;
		size_m1->len = -1;
		node* size_0 = create_node();
		size_0->suffix_link = 0; // 親
		size_0->len = 0;

		active_idx = 0;
	}

	void add(char ch) {
		s.push_back(ch);

		const int a = find_prev_palindrome_idx(active_idx);
		const auto inserted_result = c[a].link.insert(make_pair(ch, int(c.size())));
		active_idx = inserted_result.first->second;
		if (!inserted_result.second) {
			c[active_idx].count++;
			return;
		}

		node* nnode = create_node();
		nnode->count = 1;
		nnode->len = c[a].len + 2;
		if (nnode->len == 1) {
			nnode->suffix_link = 1;
		}
		else {
			const int b = find_prev_palindrome_idx(c[a].suffix_link);
			nnode->suffix_link = c[b].link[ch];
		}
	}
};

VL f(const PalindromicTree & eertree) {
	VL freq(SZ(eertree.c));
	FORR(i, SZ(eertree.c) - 1, 0) {
		freq[i] += eertree.c[i].count;
		freq[eertree.c[i].suffix_link] += freq[i];
	}
	return freq;
}
void clearCnt(PalindromicTree & eertree) {
	FOR(i, 0, SZ(eertree.c)) {
		eertree.c[i].count = 0;
	}
}

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

	string s, t;
	cin >> s >> t;
	PalindromicTree eertree;
	for (auto c : s) {
		eertree.add(c);
	}
	VL cnt1 = f(eertree);
	clearCnt(eertree);
	eertree.add('#');
	eertree.add('@');
	for (auto c : t) {
		eertree.add(c);
	}
	VL cnt2 = f(eertree);
	LL ans = 0;
	FOR(i, 2, SZ(cnt1)) { //最初の2つは特殊
		ans += cnt1[i] * cnt2[i];
	}
	cout << ans << "\n";

	return 0;
}
0