結果
問題 | No.263 Common Palindromes Extra |
ユーザー |
|
提出日時 | 2016-08-31 15:45:54 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 337 ms / 2,000 ms |
コード長 | 1,637 bytes |
コンパイル時間 | 1,507 ms |
コンパイル使用メモリ | 169,464 KB |
実行使用メモリ | 177,472 KB |
最終ジャッジ日時 | 2024-11-29 23:13:10 |
合計ジャッジ時間 | 3,367 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 12 |
ソースコード
#include <bits/stdc++.h> using namespace std; struct node { map<char, node *> next; 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++) { char c = s[i]; while (true) { if (i - curr->len - 1 >= 0 && s[i - curr->len - 1] == s[i]) break; curr = curr->suffix_link; } if (curr->next.count(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) { long long res = x->len >= 1 ? (long long)x->cnt * y->cnt : 0; for (char i = 'A'; i <= 'Z'; i++) { if (x->next.count(i) && y->next.count(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; }