Official

G - Edit to Match Editorial by en_translator


Consider the following Dynamic Programming (DP):

\(d_k[T]=(\)the minimum length of a string among \(S_1,S_2,\ldots,S_{k-1}\), and the empty string, of which \(T\) is a prefix. If there is no such string, \(\infty\) \()\).

For \(k=0\), let \(d_0[T]=0\) if \(T\) is an empty string, and \(d_0[T]=\infty\) if \(d_0[T]=\infty\).

Consider the transition from \(d_{k-1}\) to \(d_k\). For \(i=1,2,\ldots,|S_k|\), we have \(d_{k}[S_k[:i]] \leftarrow \min(d_{k}[S_k[:i]] , |S_k|)\), where \(S_k[:i]\) denotes the substring up to the \(i\)-th character of \(S_k\).

If you naively use such strings as indices, it will lead to TLE (Time Limit Exceeded); but one can store the values in a Trie and update in-place to reduce the spacial complexity to \(\displaystyle O\left(\sum_{k=1}^N |S_k| \right)\). By maintaining it on a Trie, the transitions can be processed in \(\displaystyle O(|S_k|)\) by traversing it from the root for each \(k\).

The answer is \(\displaystyle \min_{0\le i\le |S_k|} \left(|S_k|+d_{k-1}[S_{k}[:i]]-2i \right) \), which can also be computed in the same manner as the update from \(d_{k-1}\) to \(d_k\).

The problem can be solved by appropriately implementing the procedure above.

Sample code (Python3)

d = [0]
INF = 10**9
to = [[-1 for i in range(26)]]
for _ in range(int(input())):
    s = input()
    n = len(s)
    ans = n
    now = 0
    for i in range(1, n + 1):
        si = ord(s[i - 1]) - ord("a")
        if to[now][si] == -1:
            to[now][si] = len(to)
            to.append([-1 for _ in range(26)])
            d.append(INF)
        now = to[now][si]
        ans = min(ans, d[now] + n - 2 * i)
        d[now] = min(d[now], n)
    print(ans)

posted:
last update: