Password Uva1262 KMP

D - Password

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

Submit 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 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;
}

时间: 2024-12-31 14:23:43

Password Uva1262 KMP的相关文章

Codeforces A. Password(KMP的nxt跳转表)

题目描述: Password time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Asterix, Obelix and their temporary buddies Suffix and Prefix has finally found the Harmony temple. However, its doors were f

CF126B Password【KMP】By cellur925

题目传送门 其实$Chemist$在之前写了非常棒的题解! 我长话短说,补充两句. "那么当$next[n]$>$max$时显然不能将$next[n]$作为最长子串的长度"这句话其实在说,因为一个合法的串结尾肯定在2~$n$-1中,而$next[n]$的结尾在$n$处,显然是达不到的.那么我们就要继续缩小范围.直到存在合法串. $next[n]$等于0时无解?根据$next$数组的含义,我们知道它连相同前缀和后缀的匹配不上(没有),何谈中间部分的子串? 输出的时候找到合法的地点就

126B Password[扩展kmp学习]

题目大意 给你一个字符串,求它的一个子串使得这个子串即使前缀又是后缀又出现在不是前缀且不是后缀的地方 分析 扩展kmp就是定义z[i]表示i~n的子串与整个串的最长公共前缀的长度是z[i] 所以这个题就是找到一个位置使得z[i]=n-i+1 这样保证了是前缀和后缀 然后再判断之前是否有一个z[j]=z[i] 有的话代表这个长度的串在中间也出现过 直接输出这个即可 代码 #include<iostream> #include<cstdio> #include<cstring&g

Tricky and Clever Password 【KMP+Manacher】【蓝桥杯试题】

问题描述 在年轻的时候,我们故事中的英雄--国王 Copa--他的私人数据并不是完全安全地隐蔽.对他来说是,这不可接受的.因此,他发明了一种密码,好记又难以破解.后来,他才知道这种密码是一个长度为奇数的回文串. Copa 害怕忘记密码,所以他决定把密码写在一张纸上.他发现这样保存密码不安全,于是他决定按下述方法加密密码:他选定一个整数 X ,保证 X 不小于 0 ,且 2X 严格小于串长度.然后他把密码分成 3 段,最前面的 X 个字符为一段,最后面的 X 个字符为一段,剩余的字符为一段.不妨把

[UVA1262]Password

Password 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

codeforces #30E Tricky and Clever Password KMP+Manacher+二分

题目大意:给定一个字符串S,要求分成A+prefix+B+middle+C+suffix6段,满足: |prefix|=|suffix| |middle|为奇数 prefix+middle+suffix为回文串 除middle外所有段长度都可以为0 要求最大化|prefix|+|middle|+|suffix|,输出一组方案(|prefix|=|suffix|=0时只输出middle) 首先我们发现suffix串是顶着右端点的,因此我们可以枚举|suffix| 对于每个|suffix|我们需要求

Codeforces 126B. Password (KMP)

<题目链接> 题目大意:给定一个字符串,从中找出一个前.中.后缀最长公共子串("中"代表着既不是前缀,也不是后缀的部分). 解题分析:本题依然是利用了KMP中next数组的性质.具体做法见代码. #include <bits/stdc++.h> using namespace std; const int N = 1e6+5; char str[N]; int vis[N],nxt[N]; void getNext(int len){ int j=0,k=-1;

UVa1262 - Password(暴力枚举)

题意: 给出两个6行5列的字母矩阵,一个密码满足:密码的第i个字母在两个字母矩阵的第i列均出现. 然后找出字典序为k的密码,如果不存在输出NO 分析: 我们先统计分别在每一列均在两个矩阵出现的字母,然后从小到大排好序. 对于第一个样例来说,我们得到ACDW.BOP.GMOX.AP.GSU 则一共有4×3×4×2×3=288种密码,我们先计算这个数列的后缀积:288.72.24.6.3.1 要确定第一个字母,如果1≤k≤72,则是A:如果73≤k≤144,则是C,以此类推. 确定第二个字母是类似的

Codeforces 526D Tanya and Password kmp

题意:给你一个字符串 ,问你前对于任意一个前缀能不能组成  A+B+A+B...+B+A 这种形式. 解题思路:在next数组上面乱搞,判断前缀是否循环 ,循环是否为K还是K+1,为K的时候往后DP看最多能符合条件的前缀串. 解题代码: 1 // File Name: d.cpp 2 // Author: darkdream 3 // Created Time: 2015年04月06日 星期一 15时36分38秒 4 5 #include<vector> 6 #include<list&