[Vijos 1677] Tata's name (KMP)

Source: Internet
Author: User

Background

Tata is an intelligent robot, he can think like a person, but because of the IQ problem, he gave himself a long long name.
Describe

One day, Tata wanted to paint his name on the wall. Because his name was too long, for the sake of convenience, he intercepted a section of his name as a template. We may as well set the length of this template is L, Tata the length of the name is L, then there is 1≤l≤l. Then Tata will use this template for a number of spraying, squirting his name (the next spray will cover the results of the previous spray, such as the current wall already has ABC three characters, then if you spray at C, you will get ABABC). Tata spray name always from the front of the spray, assuming that Tata spraying K times, this K-spraying in chronological order the position of the first spraying is s[i], then s[i]

 #include <cstdio> #include <cmath> #include <cstring> #include <
algorithm> #include <stack> #include <iostream> using namespace std;
Char t[1000100];
int Len;
int next[1000100];
int ans[1000100];
    void Makenext () {next[1]=0;
    int i=1,j=0; while (I<=len) {if (j==0| |
            T[i]==t[j]) {i++;j++;
        Next[i]=j;
    } else j=next[j];
    }} int main () {//freopen ("In.txt", "R", stdin);
        scanf ("%s", t+1);
        Len=strlen (t+1);
        Makenext ();
        int k;
                for (int i=len+1;i>=1;i--) if (next[i]==1) {//from the next[i]==1 to the back of the case k=i-1;  
            Break
        } int ans=len;//to judge the situation out of the while (Next[ans+1]-1>=k) ans=next[ans+1]-1;
    printf ("%d", ans);
return 0; }

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.