結果
問題 | No.263 Common Palindromes Extra |
ユーザー | Yang33 |
提出日時 | 2018-08-31 10:31:52 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
MLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 3,274 bytes |
コンパイル時間 | 1,993 ms |
コンパイル使用メモリ | 182,504 KB |
実行使用メモリ | 227,848 KB |
最終ジャッジ日時 | 2024-05-07 08:05:35 |
合計ジャッジ時間 | 3,461 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 6 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 2 ms
6,944 KB |
testcase_03 | AC | 13 ms
6,944 KB |
testcase_04 | AC | 59 ms
7,204 KB |
testcase_05 | AC | 47 ms
6,940 KB |
testcase_06 | AC | 8 ms
6,940 KB |
testcase_07 | AC | 134 ms
63,440 KB |
testcase_08 | AC | 127 ms
63,072 KB |
testcase_09 | AC | 207 ms
119,492 KB |
testcase_10 | MLE | - |
testcase_11 | AC | 45 ms
6,176 KB |
ソースコード
#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!いごす ----解説ここまで---- */ #include<unordered_map> struct PalindromicTree { struct node { unordered_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; }