結果
| 問題 |
No.263 Common Palindromes Extra
|
| コンテスト | |
| ユーザー |
nok0
|
| 提出日時 | 2022-11-30 11:46:18 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,539 bytes |
| コンパイル時間 | 2,508 ms |
| コンパイル使用メモリ | 219,916 KB |
| 最終ジャッジ日時 | 2025-02-09 02:41:11 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 9 TLE * 3 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, x) for(int i = 0; i < (x); i++)
#define rng(i, l, r) for(int i = (l); i < (r); i++)
using vi = vector<int>;
#define sz(x) (int)(x).size()
array<vi, 2> manacher(string &s) {
int n = sz(s);
array<vi, 2> p = {vi(n + 1), vi(n)};
rep(z, 2) for(int i = 0, l = 0, r = 0; i < n; i++) {
int t = r - i + !z;
if(i < r) p[z][i] = min(t, p[z][l + t]);
int L = i - p[z][i], R = i + p[z][i] - !z;
while(L >= 1 and R + 1 < n and s[L - 1] == s[R + 1])
p[z][i]++, L--, R++;
if(R > r) l = L, r = R;
}
return p;
}
typedef uint64_t ull;
struct H {
ull x;
H(ull x = 0) : x(x) {}
H operator+(H o) { return x + o.x + (x + o.x < x); }
H operator-(H o) { return *this + ~o.x; }
H operator*(H o) {
auto m = (__uint128_t)x * o.x;
return H((ull)m) + (ull)(m >> 64);
}
ull get() const { return x + !~x; }
bool operator==(H o) const { return get() == o.get(); }
bool operator<(H o) const { return get() < o.get(); }
};
static const H C = (ll)1e11 + 3;
struct hash_interval {
vector<H> ha, pw;
hash_interval(string &str) : ha(sz(str) + 1), pw(ha) {
pw[0] = 1;
rep(i, sz(str))
ha[i + 1] = ha[i] * C + str[i],
pw[i + 1] = pw[i] * C;
}
H get(int a, int b) { return ha[b] - ha[a] * pw[b - a]; }
};
map<ull, ll> palindromes_freq(string &s) {
auto sm = manacher(s);
hash_interval sh(s);
set<ull> st;
map<ull, ull> es;
vector<set<ull>> nodes(sz(s) + 1);
map<ull, ll> cum;
rep(i, sz(s) + 1) {
if(sm[0][i]) {
int len = sm[0][i];
auto ha = sh.get(i - len, i + len);
cum[ha.get()]++;
nodes[len * 2].insert(ha.get());
while(len > 1) {
len--;
auto nx = sh.get(i - len, i + len);
if(es.count({ha.get()})) break;
es[ha.get()] = nx.get();
ha = nx;
nodes[len * 2].insert(ha.get());
}
}
}
rep(i, sz(s)) {
sm[1][i]++;
int len = sm[1][i];
auto ha = sh.get(i - len + 1, i + len);
cum[ha.get()]++;
nodes[len * 2 - 1].insert(ha.get());
while(len > 1) {
len--;
auto nx = sh.get(i - len + 1, i + len);
if(es.count({ha.get()})) break;
es[ha.get()] = nx.get();
ha = nx;
nodes[len * 2 - 1].insert(ha.get());
}
}
for(int i = sz(s); i >= 0; --i) {
for(auto &v : nodes[i]) {
if(es.count(v)) {
cum[es[v]] += cum[v];
}
}
}
return cum;
}
int main() {
string s, t;
cin >> s >> t;
auto s_freq = palindromes_freq(s);
auto t_freq = palindromes_freq(t);
ll res = 0;
for(auto [key, cnt] : s_freq) res += cnt * t_freq[key];
cout << res << endl;
}
nok0