码迷,mamicode.com
首页 > 其他好文 > 详细

Password Uva1262 KMP

时间:2014-11-03 10:19:51      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:uva1262   password   kmp   

D - Password
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

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 string s, carved on a rock below the temple‘s gates. Asterix supposed that that‘s the password that opens the temple and read the string aloud. However, nothing happened. Then Asterix supposed that a password is some substring t of the string s.

Prefix supposed that the substring t is the beginning of the string s; Suffix supposed that the substring t should be the end of the string s; and Obelix supposed that t should be located somewhere inside the string s, that is, t is neither its beginning, nor its end.

Asterix chose the substring t so 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 substring t aloud, the temple doors opened.

You know the string s. Find the substring t or determine that such substring does not exist and all that‘s been written above is just a nice legend.

Input

You are given the string s whose length can vary from 1 to 106 (inclusive), consisting of small Latin letters.

Output

Print the string t. If a suitable t string does not exist, then print "Just a legend" without the quotes.

Sample Input

Input
fixprefixsuffix
Output
fix
Input
abcdabc
Output
Just a legend

题意:给出一个字符串,求出最大的前缀和后缀且它能在串的中部找到,若存在则输出,否则输出
Just a legend
思路:KMPnext数组的应用,一些细节要非常注意!
代码:
#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-6
typedef 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

标签:uva1262   password   kmp   

原文地址:http://blog.csdn.net/u014422052/article/details/40718359

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!