結果

問題 No.263 Common Palindromes Extra
ユーザー pekempeypekempey
提出日時 2016-08-31 15:41:02
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 1,602 bytes
コンパイル時間 1,321 ms
コンパイル使用メモリ 162,640 KB
実行使用メモリ 255,700 KB
最終ジャッジ日時 2024-04-26 21:32:58
合計ジャッジ時間 3,433 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 9 ms
5,376 KB
testcase_04 AC 40 ms
7,716 KB
testcase_05 AC 30 ms
6,624 KB
testcase_06 AC 5 ms
5,376 KB
testcase_07 AC 190 ms
138,060 KB
testcase_08 AC 182 ms
137,096 KB
testcase_09 MLE -
testcase_10 MLE -
testcase_11 AC 29 ms
5,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct node {
	node *next[26] = {};
	node *suffix_link = nullptr;
	int len = 0;
	int cnt = 0;
	int start = 0;
};

struct PalindromicTree {
	string s;

	node *odd, *even;

	PalindromicTree(string s) : s(s) {
		odd = new node();
		even = new node();

		odd->len = -1;
		odd->suffix_link = odd;

		even->len = 0;
		even->suffix_link = odd;

		vector<node *> nodes;
		node *curr = even;

		for (int i = 0; i < s.size(); i++) {
			int c = s[i] - 'A';

			while (true) {
				if (i - curr->len - 1 >= 0 && s[i - curr->len - 1] == s[i]) break;
				curr = curr->suffix_link;
			}

			if (curr->next[c]) {
				curr = curr->next[c];
				curr->cnt++;
				continue;
			}

			node *v = curr;
			curr->next[c] = new node();
			curr->next[c]->len = curr->len + 2;
			curr = curr->next[c];
			curr->start = i - curr->len + 1;
			nodes.push_back(curr);

			if (curr->len == 1) {
				curr->suffix_link = even;
				curr->cnt = 1;
				continue;
			}

			while (true) {
				v = v->suffix_link;
				if (i - v->len - 1 >= 0 && s[i - v->len - 1] == s[i]) break;
			}

			curr->suffix_link = v->next[c];
			curr->cnt++;
		}

		for (int i = (int)nodes.size() - 1; i >= 0; i--) {
			nodes[i]->suffix_link->cnt += nodes[i]->cnt;
		}
	}
};

long long dfs(node *x, node *y) {
	if (!x || !y) return 0;
	long long res = x->len >= 1 ? (long long)x->cnt * y->cnt : 0;
	for (int i = 0; i < 26; i++) {
		res += dfs(x->next[i], y->next[i]);
	}
	return res;
}

int main() {
	string s, t;
	cin >> s >> t;

	PalindromicTree a(s), b(t);

	cout << dfs(a.even, b.even) + dfs(a.odd, b.odd) << endl;
}
0