Returns a string that is known to be a substring of an infinitely long string consisting of a string s that is repeatedly repeated. Evaluate the shortest length of S.
Idea: using the KMP algorithm, the answer is n-next [N]. The proof is as follows:
The figure is too scum...
Next [N] <= n/2.
In short, the original string can be divided into the suffix of S and several segments of S, so it is a valid answer; and KMP ensures that next [N] is as big as possible, therefore, N-next [N] is the minimum answer.
Code:
# Include <cstdio> # include <cstring> # include <cctype> # include <iostream> # include <algorithm> using namespace STD; # define n 1000010 char s [N]; int NXT [N]; int main () {int Len; scanf ("% d", & Len); scanf ("% s", S + 1); int I, j = 0; for (I = 2; I <= Len; ++ I) {While (J & S [J + 1]! = S [I]) J = NXT [J]; If (s [J + 1] = s [I]) ++ J; NXT [I] = J ;} printf ("% d", len-NXT [Len]); Return 0 ;}
Bzoj1355: [baltic2009] Radio Transmission