< topic links >
Main topic:
There is a string a, rewrite a A, will get a new string aaaaaaaa ...., now cut this string from a portion to get a string B, for example a string a= "ABCDEFG". Copy a few times to get ABCDEFGABCDEFGABCDEFGABCDEFG ...., now cut the middle red part, get the string B, now just give the string B, and find out the length of the string A.
Problem Solving Analysis:
It is not difficult to find that the definition of a is actually the shortest circular section in the string definition, so directly with KMP to find the B string in the Loop section can be.
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intM =1e6+7;CharS[m];intNxt[m];voidGetNext () {intI=0, j=-1; nxt[0]=-1; while(S[i]) {if(j==-1|| s[i]==S[j]) nxt[++i]=++J; Elsej=Nxt[j]; }}intMain () { while(gets (s)) {getnext (); intlen=strlen (s); intres=len-Nxt[len]; printf ("%d\n", RES); } return 0;}
2018-10-02
Hust 1010 The Minimum Length (circular section) "KMP"