結果
問題 | No.263 Common Palindromes Extra |
ユーザー | Yang33 |
提出日時 | 2018-08-31 10:30:34 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 171 ms / 2,000 ms |
コード長 | 3,241 bytes |
コンパイル時間 | 1,697 ms |
コンパイル使用メモリ | 181,292 KB |
実行使用メモリ | 126,600 KB |
最終ジャッジ日時 | 2024-11-29 23:15:06 |
合計ジャッジ時間 | 3,162 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 12 |
ソースコード
#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; }