結果
問題 | No.263 Common Palindromes Extra |
ユーザー | mtsd |
提出日時 | 2023-11-03 23:12:18 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 18,813 bytes |
コンパイル時間 | 3,796 ms |
コンパイル使用メモリ | 239,984 KB |
実行使用メモリ | 156,948 KB |
最終ジャッジ日時 | 2024-09-25 21:25:21 |
合計ジャッジ時間 | 11,783 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 81 ms
13,952 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 4 ms
5,376 KB |
testcase_03 | AC | 350 ms
15,612 KB |
testcase_04 | TLE | - |
testcase_05 | AC | 1,581 ms
69,508 KB |
testcase_06 | AC | 151 ms
8,960 KB |
testcase_07 | TLE | - |
testcase_08 | -- | - |
testcase_09 | -- | - |
testcase_10 | -- | - |
testcase_11 | -- | - |
ソースコード
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define inf 1000000007 #define MP make_pair #define MT make_tuple #define PB push_back #define fi first #define se second #define rep(i,n) for(int i = 0; i < (int)(n); ++i) #define rrep(i,n) for(int i = (int)n-1; i >= 0; --i) #define srep(i,a,b) for(int i = (int)a; i < (int)(b); ++i) #define all(x) (x).begin(),(x).end() #define SUM(v) accumulate(all(v), 0LL) #define MIN(v) *min_element(all(v)) #define MAX(v) *max_element(all(v)) #define lb(c, x) distance((c).begin(), lower_bound(all(c), (x))) #define ub(c, x) distance((c).begin(), upper_bound(all(c), (x))) #define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end()) #define SZ(c) (int)(c).size() template<typename T> ostream& operator << (ostream& os, vector<T>& vec) { os << "{"; for (int i = 0; i<(int)vec.size(); i++) { os << vec[i] << (i + 1 == (int)vec.size() ? "" : ", "); } os << "}"; return os; } // pair出力 template<typename T, typename U> ostream& operator << (ostream& os, pair<T, U> pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } // map出力 template<typename T, typename U> ostream& operator << (ostream& os, map<T, U>& map_var) { os << "{"; for(auto itr = map_var.begin(); itr != map_var.end(); itr++){ os << "(" << itr->first << ", " << itr->second << ")"; itr++; if(itr != map_var.end()) os << ", "; itr--; } os << "}"; return os; } // set 出力 template<typename T> ostream& operator << (ostream& os, set<T>& set_var) { os << "{"; for(auto itr = set_var.begin(); itr != set_var.end(); itr++){ os << (*itr); ++itr; if(itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } // tuple 出力 template<int N,class Tuple> void out(ostream &os,const Tuple &t){} template<int N,class Tuple,class H,class ...Ts> void out(ostream &os,const Tuple &t){ if(N)os<<", "; os<<get<N>(t); out<N+1,Tuple,Ts...>(os,t); } template<class ...Ts> ostream& operator<<(ostream &os, const tuple<Ts...> &t){ os<<"("; out<0,tuple<Ts...>,Ts...>(os,t); os<<")"; return os; } #define overload2(_1, _2, name, ...) name #define vec(type, name, ...) vector<type> name(__VA_ARGS__) #define VEC(type, name, size) \ vector<type> name(size); \ IN(name) #define vv(type, name, h, ...) vector<vector<type>> name(h, vector<type>(__VA_ARGS__)) #define VV(type, name, h, w) \ vector<vector<type>> name(h, vector<type>(w)); \ IN(name) #define vvv(type, name, h, w, ...) vector<vector<vector<type>>> name(h, vector<vector<type>>(w, vector<type>(__VA_ARGS__))) #define vvvv(type, name, a, b, c, ...) \ vector<vector<vector<vector<type>>>> name(a, vector<vector<vector<type>>>(b, vector<vector<type>>(c, vector<type>(__VA_ARGS__)))) #define INT(...) \ int __VA_ARGS__; \ IN(__VA_ARGS__) #define LL(...) \ ll __VA_ARGS__; \ IN(__VA_ARGS__) #define STR(...) \ string __VA_ARGS__; \ IN(__VA_ARGS__) #define CHR(...) \ char __VA_ARGS__; \ IN(__VA_ARGS__) #define DBL(...) \ double __VA_ARGS__; \ IN(__VA_ARGS__) int scan() { return getchar(); } void scan(int &a) { cin >> a; } void scan(long long &a) { cin >> a; } void scan(char &a) { cin >> a; } void scan(double &a) { cin >> a; } void scan(string &a) { cin >> a; } template <class T, class S> void scan(pair<T, S> &p) { scan(p.first), scan(p.second); } template <class T> void scan(vector<T> &); template <class T> void scan(vector<T> &a) { for(auto &i : a) scan(i); } template <class T> void scan(T &a) { cin >> a; } void IN() {} template <class Head, class... Tail> void IN(Head &head, Tail &...tail) { scan(head); IN(tail...); } const string YESNO[2] = {"NO", "YES"}; const string YesNo[2] = {"No", "Yes"}; const string yesno[2] = {"no", "yes"}; void YES(bool t = 1) { cout << YESNO[t] << endl; } void NO(bool t = 1) { YES(!t); } void Yes(bool t = 1) { cout << YesNo[t] << endl; } void No(bool t = 1) { Yes(!t); } void yes(bool t = 1) { cout << yesno[t] << endl; } void no(bool t = 1) { yes(!t); } #ifdef LOCAL void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...); } #define dbg(...) \ cerr << __LINE__ << " [" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl #else #define dbg(...) (void(0)) #define dump(x) (void(0)) #endif template<typename A, typename T> std::enable_if_t<std::is_convertible<T, A>::value> fill(A& array, const T& val) { array = val; } template<typename A, typename T> std::enable_if_t<!std::is_convertible<T, A>::value> fill(A& array, const T& val) { for (auto& a : array) { fill(a, val); } } template <typename T, typename S> T ceil(T x, S y) { assert(y); return (y < 0 ? ceil(-x, -y) : (x > 0 ? (x + y - 1) / y : x / y)); } template <typename T, typename S> T floor(T x, S y) { assert(y); return (y < 0 ? floor(-x, -y) : (x > 0 ? x / y : x / y - (x % y == 0 ? 0 : 1))); } vector<int> iota(int n) {vector<int> a(n);iota(all(a), 0);return a;} template <class T> T POW(T x, int n) {T res = 1;for(; n; n >>= 1, x *= x){if(n & 1) res *= x;}return res;} ll pow2(int i) { return 1LL << i; } int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); } int topbit(ll t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); } int lowbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); } int lowbit(ll a) { return a == 0 ? 64 : __builtin_ctzll(a); } // int allbit(int n) { return (1 << n) - 1; } ll allbit(ll n) { return (1LL << n) - 1; } int popcount(signed t) { return __builtin_popcount(t); } int popcount(ll t) { return __builtin_popcountll(t); } bool ispow2(int i) { return i && (i & -i) == i; } template <class S> void fold_in(vector<S> &v) {} template <typename Head, typename... Tail, class S> void fold_in(vector<S> &v, Head &&a, Tail &&...tail) { for(auto e : a) v.emplace_back(e); fold_in(v, tail...); } template <class S> void renumber(vector<S> &v) {} template <typename Head, typename... Tail, class S> void renumber(vector<S> &v, Head &&a, Tail &&...tail) { for(auto &&e : a) e = lb(v, e); renumber(v, tail...); } template <class S, class... Args> void zip(vector<S> &head, Args &&...args) { vector<S> v; fold_in(v, head, args...); sort(all(v)), v.erase(unique(all(v)), v.end()); renumber(v, head, args...); } template<class T> inline bool chmax(T &a, T b){ if(a<b){ a = b; return true; } return false; } template<class T> inline bool chmin(T &a, T b){ if(a>b){ a = b; return true; } return false; } std::vector<int> sa_naive(const std::vector<int>& s) { int n = int(s.size()); std::vector<int> sa(n); std::iota(sa.begin(), sa.end(), 0); std::sort(sa.begin(), sa.end(), [&](int l, int r) { if (l == r) return false; while (l < n && r < n) { if (s[l] != s[r]) return s[l] < s[r]; l++; r++; } return l == n; }); return sa; } std::vector<int> sa_doubling(const std::vector<int>& s) { int n = int(s.size()); std::vector<int> sa(n), rnk = s, tmp(n); std::iota(sa.begin(), sa.end(), 0); for (int k = 1; k < n; k *= 2) { auto cmp = [&](int x, int y) { if (rnk[x] != rnk[y]) return rnk[x] < rnk[y]; int rx = x + k < n ? rnk[x + k] : -1; int ry = y + k < n ? rnk[y + k] : -1; return rx < ry; }; std::sort(sa.begin(), sa.end(), cmp); tmp[sa[0]] = 0; for (int i = 1; i < n; i++) { tmp[sa[i]] = tmp[sa[i - 1]] + (cmp(sa[i - 1], sa[i]) ? 1 : 0); } std::swap(tmp, rnk); } return sa; } // SA-IS, linear-time suffix array construction // Reference: // G. Nong, S. Zhang, and W. H. Chan, // Two Efficient Algorithms for Linear Time Suffix Array Construction template <int THRESHOLD_NAIVE = 10, int THRESHOLD_DOUBLING = 40> std::vector<int> sa_is(const std::vector<int>& s, int upper) { int n = int(s.size()); if (n == 0) return {}; if (n == 1) return {0}; if (n == 2) { if (s[0] < s[1]) { return {0, 1}; } else { return {1, 0}; } } if (n < THRESHOLD_NAIVE) { return sa_naive(s); } if (n < THRESHOLD_DOUBLING) { return sa_doubling(s); } std::vector<int> sa(n); std::vector<bool> ls(n); for (int i = n - 2; i >= 0; i--) { ls[i] = (s[i] == s[i + 1]) ? ls[i + 1] : (s[i] < s[i + 1]); } std::vector<int> sum_l(upper + 1), sum_s(upper + 1); for (int i = 0; i < n; i++) { if (!ls[i]) { sum_s[s[i]]++; } else { sum_l[s[i] + 1]++; } } for (int i = 0; i <= upper; i++) { sum_s[i] += sum_l[i]; if (i < upper) sum_l[i + 1] += sum_s[i]; } auto induce = [&](const std::vector<int>& lms) { std::fill(sa.begin(), sa.end(), -1); std::vector<int> buf(upper + 1); std::copy(sum_s.begin(), sum_s.end(), buf.begin()); for (auto d : lms) { if (d == n) continue; sa[buf[s[d]]++] = d; } std::copy(sum_l.begin(), sum_l.end(), buf.begin()); sa[buf[s[n - 1]]++] = n - 1; for (int i = 0; i < n; i++) { int v = sa[i]; if (v >= 1 && !ls[v - 1]) { sa[buf[s[v - 1]]++] = v - 1; } } std::copy(sum_l.begin(), sum_l.end(), buf.begin()); for (int i = n - 1; i >= 0; i--) { int v = sa[i]; if (v >= 1 && ls[v - 1]) { sa[--buf[s[v - 1] + 1]] = v - 1; } } }; std::vector<int> lms_map(n + 1, -1); int m = 0; for (int i = 1; i < n; i++) { if (!ls[i - 1] && ls[i]) { lms_map[i] = m++; } } std::vector<int> lms; lms.reserve(m); for (int i = 1; i < n; i++) { if (!ls[i - 1] && ls[i]) { lms.push_back(i); } } induce(lms); if (m) { std::vector<int> sorted_lms; sorted_lms.reserve(m); for (int v : sa) { if (lms_map[v] != -1) sorted_lms.push_back(v); } std::vector<int> rec_s(m); int rec_upper = 0; rec_s[lms_map[sorted_lms[0]]] = 0; for (int i = 1; i < m; i++) { int l = sorted_lms[i - 1], r = sorted_lms[i]; int end_l = (lms_map[l] + 1 < m) ? lms[lms_map[l] + 1] : n; int end_r = (lms_map[r] + 1 < m) ? lms[lms_map[r] + 1] : n; bool same = true; if (end_l - l != end_r - r) { same = false; } else { while (l < end_l) { if (s[l] != s[r]) { break; } l++; r++; } if (l == n || s[l] != s[r]) same = false; } if (!same) rec_upper++; rec_s[lms_map[sorted_lms[i]]] = rec_upper; } auto rec_sa = sa_is<THRESHOLD_NAIVE, THRESHOLD_DOUBLING>(rec_s, rec_upper); for (int i = 0; i < m; i++) { sorted_lms[i] = lms[rec_sa[i]]; } induce(sorted_lms); } return sa; } std::vector<int> suffix_array(const std::string& s) { int n = int(s.size()); std::vector<int> s2(n); for (int i = 0; i < n; i++) { s2[i] = s[i]; } return sa_is(s2, 255); } template <class T> std::vector<int> lcp_array(const std::vector<T>& s, const std::vector<int>& sa) { int n = int(s.size()); assert(n >= 1); std::vector<int> rnk(n); for (int i = 0; i < n; i++) { rnk[sa[i]] = i; } std::vector<int> lcp(n - 1); int h = 0; for (int i = 0; i < n; i++) { if (h > 0) h--; if (rnk[i] == 0) continue; int j = sa[rnk[i] - 1]; for (; j + h < n && i + h < n; h++) { if (s[j + h] != s[i + h]) break; } lcp[rnk[i] - 1] = h; } return lcp; } std::vector<int> lcp_array(const std::string& s, const std::vector<int>& sa) { int n = int(s.size()); std::vector<int> s2(n); for (int i = 0; i < n; i++) { s2[i] = s[i]; } return lcp_array(s2, sa); } template<typename T> class segtree { private: int n,sz,h; vector<T> node, lazy_update, lazy_add; vector<bool> lazyFlag; public: segtree(vector<T> v) : n(1), sz((int)v.size()), h(0){ while(n < sz) n *= 2, h++; node.resize(2*n, 0); lazy_update.resize(2*n, 0); lazyFlag.resize(2*n, false); lazy_add.resize(2*n, 0); for(int i = 0; i < sz; i++) node[i+n] = v[i]; for(int i=n-1; i>=1; i--) node[i] = node[2*i] + node[2*i+1]; } void eval(int k) { if(lazyFlag[k]){ lazy_update[k] += lazy_add[k]; node[k] = lazy_update[k]; if(k < n) { lazy_add[2*k] = lazy_add[2*k+1] = 0; lazy_update[2*k] = lazy_update[2*k+1] = lazy_update[k] / 2; lazyFlag[2*k] = lazyFlag[2*k+1] = true; } lazy_add[k] = 0, lazyFlag[k] = false; }else if(lazy_add[k] != 0){ node[k] += lazy_add[k]; if(k < n){ lazy_add[2*k] += lazy_add[k] / 2; lazy_add[2*k+1] += lazy_add[k] / 2; } lazy_add[k] = 0; } } void update(int a, int b, T x, int k=1, int l=0, int r=-1) { if(r < 0) r = n; eval(k); if(b <= l || r <= a) return; if(a <= l && r <= b){ lazy_update[k] = x*(r-l); lazyFlag[k] = true; eval(k); }else{ update(a, b, x, 2*k, l, (l+r)/2); update(a, b, x, 2*k+1, (l+r)/2, r); node[k] = node[2*k] + node[2*k+1]; } } void add(int a, int b, T x, int k=1, int l=0, int r=-1){ if(r < 0) r = n; eval(k); if(b <= l || r <= a) return; if(a <= l && r <= b){ lazy_add[k] += x*(r-l); eval(k); }else{ add(a, b, x, 2*k, l, (l+r)/2); add(a, b, x, 2*k+1, (l+r)/2, r); node[k] = node[2*k] + node[2*k+1]; } } T query(int a, int b) { a += n, b += n - 1; for(int i = h; i > 0; i--) eval(a >> i), eval(b >> i); b++; T res1 = 0, res2 = 0; while(a < b) { if(a & 1) eval(a), res1 += node[a++]; if(b & 1) eval(--b), res2 += node[b]; a >>= 1, b >>= 1; } return res1 + res2; } void print(){for(int i = 0; i < sz; i++)cout<<query(i,i+1)<< " ";cout<<endl;} }; //iを中心とする最長の回文の半径をR[i]に格納(O(n)) // abaab を $a$b$a$a$b$ みたいにすると偶数長のもの求めることが可能 void manacher(const string& S,vector<int>& res) { int sz = (int)S.size(), i = 0, j = 0, k; res.resize(sz); while(i < sz){ while(i-j >= 0 && i+j < sz && S[i-j] == S[i+j]) j++; res[i] = j, k = 1; while(i-k >= 0 && i+k < sz && k+res[i-k] < j){ res[i+k] = res[i-k], k++; } i += k; j -= k; } } int main(){ STR(s,t); string Q; rep(i,s.size()){ Q.push_back('$'); Q.push_back(s[i]); } Q.push_back('$'); Q.push_back('|'); int T = Q.size(); rep(i,t.size()){ Q.push_back('$'); Q.push_back(t[i]); } Q.push_back('$'); vector<int> mana; manacher(Q,mana); int mx = MAX(mana); // cerr << mana << endl; int n = Q.size(); auto sa = suffix_array(Q); auto lcp = lcp_array(Q,sa); ll res = 0; vector<ll> ppp(mx+1); segtree<ll> segS(ppp); segtree<ll> segT(ppp); // cerr << Q << endl; for(int i=0;i<sa.size();i++){ int id = sa[i]; int LCP = inf; if(i!=0)chmin(LCP,lcp[i-1]); if(Q[id]=='$'){ int len = LCP/2; segS.update(len+1,mx+1,0); segT.update(len+1,mx+1,0); }else{ int len = (LCP+1)/2; segS.update(len+1,mx+1,0); segT.update(len+1,mx+1,0); } if(Q[id]=='|')continue; if(id<T){ int len = mana[id]/2; res += segT.query(1,len+1); segS.add(0,len+1,1); }else{ int len = mana[id]/2; res += segS.query(1,len+1); segT.add(0,len+1,1); } // cerr << id << endl; // cerr << Q.substr(id) << endl; // dbg(res); } cout << res << endl; return 0; }