Submission #1008034

#TimeUsernameProblemLanguageResultExecution timeMemory
1008034Zero_OPPalindromes (APIO14_palindrome)C++14
100 / 100
65 ms59212 KiB
#include<bits/stdc++.h> using namespace std; using int64 = int64_t; #define FOR(i, l, r) for(int i = (l), _r = (r); i <= _r; ++i) #define FORD(i, r, l) for(int i = (r), _l = (l); i >= _l; --i) #define rep(i, l, r) for(int i = (l), _r = (r); i < _r; ++i) #define repd(i, r, l) for(int i = (r) - 1, _l = l; i >= _l; --i) #define left __left #define right __right #define next __next #define prev __prev #define div __div #define pb push_back #define pf push_front #define all(v) v.begin(), v.end() #define sz(v) (int)v.size() #define compact(v) v.erase(unique(all(v)), end(v)) #define dbg(v) "[" #v " = " << (v) << "]" #define file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); } template<typename T> bool minimize(T& a, const T& b){ if(a > b) return a = b, true; return false; } template<typename T> bool maximize(T& a, const T& b){ if(a < b) return a = b, true; return false; } template<int dimension, typename T> struct vec : public vector<vec<dimension - 1, T>> { static_assert(dimension > 0, "Dimension must be positive !\n"); template<typename... Args> vec(int n = 0, Args... args) : vector<vec<dimension - 1, T>> (n, vec<dimension - 1, T>(args...)) {} }; template<typename T> struct vec<1, T> : public vector<T> { vec(int n = 0, T val = T()) : vector<T>(n, val) {} }; void preprocess(); void init(); void process(); int main(){ ios_base::sync_with_stdio(0); cin.tie(0); file("antuvu"); preprocess(); int T = 1; //cin >> T; while(T--){ init(); process(); } return 0; } // Palindrome Tree {{{ // Notes: // - number of *distinct* palindrome substring <= N // Tested: // - https://oj.vnoi.info/problem/icpc22_mn_d template<int MAXC = 26> struct PalindromicTree { PalindromicTree(const string& str) : _sz(str.size() + 5), next(_sz, vector<int> (MAXC, 0)), link(_sz, 0), qlink(_sz, 0), cnt(_sz, 0), right_id(_sz, 0), len(_sz, 0), s(_sz, 0) { init(); for (int i = 0; i < (int) str.size(); ++i) { add(str[i], i); } count(); } int _sz; // returns vector of (left, right, frequency) vector<tuple<int,int,int>> get_palindromes() { vector<tuple<int,int,int>> res; dfs(0, res); dfs(1, res); return res; } void dfs(int u, vector<tuple<int,int,int>>& res) { if (u > 1) { // u = 0 and u = 1 are two empty nodes res.emplace_back(right_id[u] - len[u] + 1, right_id[u], cnt[u]); } for (int i = 0; i < MAXC; ++i) { if (next[u][i]) dfs(next[u][i], res); } } int last, n, p; vector<vector<int>> next, dlink; vector<int> link, qlink, cnt, right_id, len, s; int newnode(int l, int right) { len[p] = l; right_id[p] = right; return p++; } void init() { p = 0; newnode(0, -1), newnode(-1, -1); n = last = 0; s[n] = -1, link[0] = 1; } int getlink(int x) { while (s[n - len[x] - 1] != s[n]) { if (s[n - len[link[x]] - 1] == s[n]) x = link[x]; else x = qlink[x]; } return x; } void add(char c, int right) { c -= 'a'; s[++n] = c; int cur = getlink(last); if (!next[cur][(int) c]) { int now = newnode(len[cur] + 2, right); link[now] = next[getlink(link[cur])][(int) c]; next[cur][(int) c] = now; if (s[n - len[link[now]]] == s[n - len[link[link[now]]]]) { qlink[now] = qlink[link[now]]; } else { qlink[now] = link[link[now]]; } } last = next[cur][(int) c]; cnt[last]++; } void count() { for (int i = p - 1; i >= 0; i--) { cnt[link[i]] += cnt[i]; } } }; // }}} void preprocess(){ } void init(){ } void process(){ string s; cin >> s; PalindromicTree<26> T(s); vector<tuple<int, int, int>> p = T.get_palindromes(); long long ans = 0; for(int i = 0; i < sz(p); ++i){ int l, r, cnt; tie(l, r, cnt) = p[i]; maximize(ans, 1ll * cnt * (r - l + 1)); } cout << ans << '\n'; }

Compilation message (stderr)

palindrome.cpp: In function 'int main()':
palindrome.cpp:21:58: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 | #define    file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
palindrome.cpp:55:5: note: in expansion of macro 'file'
   55 |     file("antuvu");
      |     ^~~~
palindrome.cpp:21:91: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 | #define    file(name) if(fopen(name".inp", "r")) {freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
      |                                                                                    ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
palindrome.cpp:55:5: note: in expansion of macro 'file'
   55 |     file("antuvu");
      |     ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...