Submission #554536

#TimeUsernameProblemLanguageResultExecution timeMemory
554536kwongwengPalindromes (APIO14_palindrome)C++17
0 / 100
64 ms71252 KiB
/* Solution for APIO 2014 - Palindrome Tags : palindromic tree */ #include <bits/stdc++.h> using namespace std; #pragma GCC target ("avx2") #pragma GCC optimization ("Ofast") #pragma GCC optimization ("unroll-loops") typedef long long ll; typedef vector<int> vi; typedef pair<ll, ll> ii; typedef vector<ii> vii; typedef long double ld; typedef pair<ll, ll> pll; #define FOR(i, a, b) for(int i = a; i < b; i++) #define ROF(i, a, b) for(int i = a; i >= b; i--) #define ms memset #define pb push_back #define fi first #define se second ll MOD = 1000000007; ll power(ll base, ll n){ if (n == 0) return 1; if (n == 1) return base; ll halfn = power(base, n/2); if (n % 2 == 0) return (halfn * halfn) % MOD; return (((halfn * halfn) % MOD) * base) % MOD; } ll inverse(ll n){ return power(n, MOD-2); } ll add(ll a, ll b){ return (a+b) % MOD; } ll mul(ll a, ll b){ a %= MOD; return (a*b) % MOD; } ll gcd(ll a, ll b){ if (a == 1) return 1; if (a == 0) return b; return gcd(b%a, a); } string s; const int N = 300001; struct Node{ int len; int nxt[26]; vi edges; int sufflink; int cnt; //len = length of palindrome //nxt[i] = palindrome formed by adding ith alphabet to current palindrome //edges = list of palindromes that contains this palindrome as longest proper suffix that is a palindrome //sufflink = longest palindrome that is proper suffix of current palindrome //cnt = # occurence of this palindrome } tree[N]; int num, suff; //initialise the tree void init(){ num = 2; suff = 2; tree[0].len = -1; tree[0].sufflink = 0; tree[1].len = 0; tree[1].sufflink = 0; tree[0].edges.pb(1); } void add(int pos){ int cur = suff, curlen = 0; int letter = s[pos]-'a'; while (true){ curlen = tree[cur].len; if (pos-curlen-1 >= 0 && s[pos-curlen-1] == s[pos]){ break; } cur = tree[cur].sufflink; } num++; suff = num; tree[num].len = tree[cur].len + 2; tree[num].cnt = 1; tree[cur].nxt[letter] = num; if (tree[num].len == 1){ tree[num].sufflink = 1; tree[1].edges.pb(num); return; } while (true){ cur = tree[cur].sufflink; curlen = tree[cur].len; if (pos-curlen-1 >= 0 && s[pos-curlen-1] == s[pos]){ int val = tree[cur].nxt[letter]; tree[num].sufflink = val; tree[val].edges.pb(num); break; } } } ll ans = 0; void dfs(int u){ for (int v : tree[u].edges){ dfs(v); tree[u].cnt += tree[v].cnt; } ans = max(ans, (ll) tree[u].cnt * (ll) tree[u].len); } void solve(){ cin >> s; int n = s.size(); init(); FOR(i,0,n){ add(i); } dfs(0); cout << ans; return; } int main() { ios::sync_with_stdio(false); int TC = 1; //cin >> TC; FOR(i, 1, TC+1){ //cout << "Case #" << i << ": "; solve(); } }

Compilation message (stderr)

palindrome.cpp:10: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   10 | #pragma GCC optimization ("Ofast")
      | 
palindrome.cpp:11: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
   11 | #pragma GCC optimization ("unroll-loops")
      |
#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...