Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=3746
Give you a string that lets you add as few characters as possible in the back so that the string becomes a repeating string.
ABCA---Add BC to become ABCABC
ABCD---Add ABCD to become ABCDABCD
AA---No need to add
The classic is to find a minimum of follow-up links.
The first conclusion: the minimum loop section of a string is: Len-next[len]
#include <stdio.h>#include<iostream>#include<algorithm>#include<string.h>using namespacestd;Const intN = 1e5+7;CharS[n];intNext[n], N;voidGetNext () {intI=0, j=-1; next[0] = -1; while(i<N) {if(j==-1|| S[i] = =S[j]) next[++i] = + +J; ElseJ=Next[j]; }}intMain () {intans, T; scanf ("%d", &T); while(t--) {scanf ("%s", s); N=strlen (s); GetNext (); Ans= N-Next[n]; if(Next[n] = =0) {printf ("%d\n", N); Continue; } if(n%ans==0)///determine if it is not necessary to add;Ans =0; Elseans= Ans-next[n]% ans;///ans = 2*ans-n;printf"%d\n", ans); } return 0;}View Code
Cyclic nacklace---hdu3746 (cycle section, KMP)