Summary of training in the third week

1. KMP+Maximum and Minimum Representation

Maximum and Minimum Representation

Maximum and minimum representation is used to solve the problem of string isomorphism. In the time of complexity of $O(n) $, it finds the starting position of the largest (smallest) lexicographic sequence in all isomorphic strings of a string.

Application:

Give $n $circular strings to determine how many different strings there are: one by one in the largest (smallest) representation, and then add (Set) to de-duplicate

The representation of maximum (minimum) lexicographic order in all isomorphic strings of a cyclic string: the starting position can be obtained by using the maximum (minimum) representation and the output can be obtained.

Determine whether two strings are isomorphic: the two strings are represented by the largest (smallest) representation, and then compared character by character

Principle:

Let a string (S\) be the minimal representation of a cyclically isomorphic string of \\(S\\\\\\\\\\\\

The simplest algorithm is to set $i(,\ j (two pointers, ) i (pointing to the position of the smallest representation, J $as the comparison pointer.

Let(i=0), \(j=1), then:

If (S[i]>S[j]\), then: (i=j), \(j=i+1)

If (S[i]<S[j]\\), then:(j++\)

If (S[i]=S[j]\, then set the pointer (k\) from $i $and $j\ (position down comparison until ) S[i]!=S[j], respectively.$

If \(S[i+k]>S[j+k]\), then:(i=j), \(j=i+1\)

Otherwise, $j++$

Finally, return $i $

As you can see, the naive algorithm moves too few pointers at $S[i]=S[j] (when, ) I $and encounters something like $bbb... When bbbbbba $is such a complex string, the time complexity may degrade to (O(n^2)\. To solve this problem, we improve the algorithm to get the minimum representation of (O(n)\. The core idea is to maintain both (i) and $J pointers at the same time when $S[i]=S[j]$.

Similarly, let(i=0), (j=1), then:

If (S[i]>S[j]\), then: (i=j), \(j=i+1)

If (S[i]<S[j]\\), then:(j++\)

If $S[i]=S[j](), set the pointer \ k(), and compare it downward from \ i (and \) J $, respectively, until \(S[i]!=S[j]\

Thoughts on HDU-3374 Problem Solving

First, we decide whether it is a cyclic string or not. Then we use the minimum representation and the maximum representation to find the corresponding position. The minimum representation can find a string like a ring, find the beginning of a character, and then the dictionary order of the string is the smallest.