POJ--2752 -- seek the name, seek the fame [KMP]

Source: Internet
Author: User

Link:Http://poj.org/problem? Id = 2752

Question:For a string s, the first n characters may be equal to the last n characters, and the N values are output from small to large.


Ideas:This question deepens the understanding of the next array.Next [I + 1] is equivalent to a substring consisting of the next [I + 1] character and the previous next [I + 1] character.After understanding it, it is better to do it. First, the length of the string Len must be an answer, and then next [Len] is also an answer, for example, written in the red letter, this iteration ends until the next value is equal to 0. This is the answer from the largest to the smallest, and then output in reverse order.

Because of the special nature of this question, it is impossible to use the optimized getnext function to AC.


#include<cstring>#include<string>#include<fstream>#include<iostream>#include<iomanip>#include<cstdio>#include<cctype>#include<algorithm>#include<queue>#include<map>#include<set>#include<vector>#include<stack>#include<ctime>#include<cstdlib>#include<functional>#include<cmath>using namespace std;#define PI acos(-1.0)#define MAXN 500100#define eps 1e-7#define INF 0x7FFFFFFF#define LLINF 0x7FFFFFFFFFFFFFFF#define seed 131#define mod 1000000007#define ll long long#define ull unsigned ll#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1char str[401000];int next[401000];void getnext(){    int i = 0, j = -1;    int l = strlen(str);    next[0] = -1;    while(i<l){        if(j==-1||str[i]==str[j]){            i++;            j++;            next[i] = j;        }        else            j = next[j];    }}int ans[401000];int main(){    int i,j;    while(scanf("%s",str)!=EOF){        getnext();        int l = strlen(str);        int i = l;        int sum = 0;        while(i!=0){            ans[sum++] = i;            i = next[i];        }        printf("%d",ans[sum-1]);        for(i=sum-2;i>=0;i--){            printf(" %d",ans[i]);        }        puts("");    }}


POJ--2752 -- seek the name, seek the fame [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.