Bzoj1355: [baltic2009] Radio Transmission

Source: Internet
Author: User

Returns a string that is known to be a substring of an infinitely long string consisting of a string s that is repeatedly repeated. Evaluate the shortest length of S.


Idea: using the KMP algorithm, the answer is n-next [N]. The proof is as follows:


The figure is too scum...

Next [N] <= n/2.

In short, the original string can be divided into the suffix of S and several segments of S, so it is a valid answer; and KMP ensures that next [N] is as big as possible, therefore, N-next [N] is the minimum answer.


Code:

# Include <cstdio> # include <cstring> # include <cctype> # include <iostream> # include <algorithm> using namespace STD; # define n 1000010 char s [N]; int NXT [N]; int main () {int Len; scanf ("% d", & Len); scanf ("% s", S + 1); int I, j = 0; for (I = 2; I <= Len; ++ I) {While (J & S [J + 1]! = S [I]) J = NXT [J]; If (s [J + 1] = s [I]) ++ J; NXT [I] = J ;} printf ("% d", len-NXT [Len]); Return 0 ;}


Bzoj1355: [baltic2009] Radio Transmission

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.