Balkan OI 2015 - Clarkson

Author: Andi Qu

There are two parts to the solution:

  1. Finding the array such that the substring is a substring of and is maximal.
  2. Binary searching for the answer.

Part 1 - Finding

This is the hardest part of the problem. We will solve it using a suffix array and LCP.

First, join and together with a ~ character and build the suffix array of the resulting string. Using this suffix array, we can then build the LCP array. (If you are unfamiliar with this, check out this CodeForces course.)

Recall that the LCP of two suffixes is the range minimum of the elements of the LCP array between those two suffixes.

Since we want to be maximal, we thus only need to check two suffixes for each - the two closest suffixes to the suffix starting on ! We can find the indices of these two suffixes using a std::set.

This part of the algorithm runs in time (but slower suffix array constructions are fast enough to pass as well).

Part 2 - Binary Searching

Binary search works because if we can achieve a minimum length , then we can also achieve a minimum length (since we don't necessarily need a substring of length ; we only need the lengths of all substrings to be of length at least .)

Here's how we check whether we can achieve a certain minimum length :

Let be whether we can make using substrings from of size at least . Clearly, if is true for any , then is also true.

To check whether such a exists, we can store all indices where is true in a set and check whether any of them lie in the range .

This part of the algorithm runs in time (or time if you do the check in ).

Code

1#include <bits/stdc++.h>
2using namespace std;
3
4string s, t, u;
5int n, m, l;
6int ord[200005], nord[200005], suff[200005], rev[200005], p = 1;
7int lcp[200005][20], match[100000], h = 0;
8set<int> topgear;
9
10void build_suffix_arr() {

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 Balkan OI 2015 - Clarkson!