Hust 1010 The Minimum Length (circular section) "KMP"

Source: Internet
Author: User

< 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;}


Hust 1010 The Minimum Length (circular 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: 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.