diff --git a/include.hh b/include.hh index 4572501..671eb55 100644 --- a/include.hh +++ b/include.hh @@ -136,4 +136,14 @@ vector kmp(string s, string t) { // find all t in s } return v; } +int period(string s) { // find the length of shortest recurring period + int n = s.length(); + auto z = calc_z(s); + for (int i = 1; i <= n / 2; ++i) { + if (n % i == 0 && z[i] == n - i) { + return i; + } + } + return n; +} /////////////////////////////////////////////////////////