Password uva1262 KMP

Source: Internet
Author: User
D-Password Time limit:2000 ms Memory limit:262144kb 64bit Io format:% I64d & % i64usubmit status practice codeforces 126B

Description

Asterix, Obelix and their temporary buddies suffix and prefix has finally found the harmony temple. However, its doors were firmly locked and even Obelix had no luck opening them.

A little later they found a stringS, Carved on a rock below the temple's gates. asterix supposed that's the password that opens the temple and read the string aloud. however, nothing happened. then Asterix supposed that a password is some substringTOf the stringS.

Prefix supposed that the substringTIs the beginning of the stringS; Suffix supposed that the substringTShocould be the end of the stringS; And Obelix supposed thatTShocould be located somewhere inside the stringS, That is,TIs neither its beginning, nor its end.

Asterix chose the substringTSo as to please all his companions. Besides, from all acceptable variants Asterix chose the longest one (as Asterix loves long strings). When Asterix read the substringTAloud, the temple doors opened.

You know the stringS. Find the substringTOr determine that such substring does not exist and all that's been written abve is just a nice legend.

Input

You are given the stringSWhose length can vary from 1 to 106 (random), consisting of small Latin letters.

Output

Print the stringT. If a suitableTString does not exist, then print "just a legend" without the quotes.

Sample Input

Input
fixprefixsuffix
Output
fix
Input
abcdabc
Output
Just a legend

Returns a string with the largest prefix and suffix. It can be found in the middle of the string. If it exists, it is output. Otherwise, it is output.
Just a legend
Idea: the application of the kmpnext array. Pay attention to some details!
Code:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define maxn 1005#define MAXN 2005#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6typedef long long ll;using namespace std;int N;int nextval[1000010];char str[1000010],pstr[1000010];int get_nextval(){    int i=0;    int len=strlen(str);    int j=-1;    nextval[i]=-1;    while (i<len)    {        if (j==-1||str[i]==str[j])        {            i++;            j++;            nextval[i]=j;        }        else            j=nextval[j];    }//    for (int i=0;i<=len;i++)//        printf("%d ",nextval[i]);//    printf("\n");//    printf("%d\n",nextval[len]);    return nextval[len];}int kmp_search(){    int ans=0;    int plen=strlen(pstr);    int slen=strlen(str);    int i=0;    int j=0;    while (i<slen)    {        if (j==-1||pstr[j]==str[i])        {            i++;            j++;            if (j==plen)            {                ans++;                j=nextval[j];            }        }        else            j=nextval[j];    }    return ans;}int main(){    while (~scanf("%s",str))    {        int len=strlen(str);        int ma=get_nextval();        for (int i=0;i<ma;i++)            pstr[i]=str[i];        pstr[ma]='\0';        if (ma==0)        {            printf("Just a legend\n");            continue;        }//        printf("ma=%d\n",ma);        if (ma>=2&&ma==len-1)        {            for (int i=0;i<len-2;i++)                printf("%c",str[i]);            printf("\n");            continue;        }        int j=ma;        int ok=0;        while (j!=-1)        {            int ans=kmp_search();//            printf("ans=%d\n",ans);            if (ans>2)            {                for (int t=0;t<j;t++)                    printf("%c",str[t]);                printf("\n");                ok=1;                break;            }//            printf("j=%d\n",j);            j=nextval[j];            if (j==0)                break;            pstr[j]='\0';        }//        printf("ok=%d\n",ok);        if (!ok)            printf("Just a legend\n");    }    return 0;}


Password uva1262 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.