APIO 2014 - Palindrome

Author: Andi Qu

Solution

First, construct the palindromic tree from the string. There are distinct palindromes, so our tree will have nodes.

In addition to the standard information about a node's palindrome that we store in it (e.g. length and suffix link), store the number of times each node's palindrome was the maximum suffix. Let this number be for the -th node.

Since each node's palindrome is a substring of the palindromes of the nodes in its subtree, the sum of in node 's subtree gives us the number of occurrences of node 's palindrome in the string.

We can then do a DFS to find the number of occurrences of each distinct palindrome in the string and then we simply check which one has the greatest occurrence value.

Complexity

Time: .

Memory: .

Code

1#include <bits/stdc++.h>
2#define FOR(i, x, y) for (int i = x; i < y; i++)
3typedef long long ll;
4using namespace std;
5
6struct Node {
7 int nxt[26], sufflink;
8 ll len, cnt;
9 vector<int> edges;
10} tree[303030];

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!

Give Us Feedback on APIO 2014 - Palindrome!