Cyclic nacklace---hdu3746 (cycle section, KMP)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.