[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 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

Sample Output

Case 1: 13
Case 2: 6

Hint

找字符串中是否存在前后缀相同的字符串,KMP pre数组应用

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <cmath>
 7 #include <queue>
 8 #include <map>
 9 #include <stack>
10 #include <list>
11 #include <vector>
12
13 using namespace std;
14
15 const int maxn = 1000010;
16 int nb;
17 char b[maxn];
18 int pre[maxn];
19 int vis[maxn];
20
21 //b是模式串,a是目标串
22 void getpre(char *b, int *pre) {
23     int j, k;
24     pre[0] = -1;
25     j = 0;
26     k = -1;
27     while(j < nb) {
28         if(k == -1 || b[j] == b[k]) {
29             j++;
30             k++;
31             pre[j] = k;
32         }
33         else {
34             k = pre[k];
35         }
36     }
37 }
38
39 int main() {
40     // freopen("in", "r", stdin);
41     while(~scanf("%s", b)) {
42         memset(pre, 0, sizeof(pre));
43         memset(vis, 0, sizeof(vis));
44         nb = strlen(b);
45         if(nb < 3) {
46             printf("Just a legend\n");
47             continue;
48         }
49         getpre(b, pre);
50         for(int i = 0; i < nb; i++) {
51             vis[pre[i]] = 1;
52         }
53         int flag = 0;
54         while(pre[nb]) {
55             if(vis[pre[nb]]) {
56                 printf("%.*s\n", pre[nb], b);
57                 flag = 1;
58                 break;
59             }
60             nb = pre[nb];
61         }
62         if(!flag) {
63             printf("Just a legend\n");
64         }
65     }
66     return 0;
67 }
时间: 2024-10-21 23:51:04

[UVA1262]Password的相关文章

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,以此类推. 确定第二个字母是类似的

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,

UVA 1262 Password 暴力枚举

Password Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 1262 64-bit integer IO format: %lld      Java class name: Main Prev Submit Status Statistics Discuss Next [PDF Link] Shoulder-surfing is the behavior

输入password登录到主界面,录入学生编号,排序后输出

n 题目:输入password登录到主界面,录入学生编号,排序后输出 n 1.  语言和环境 A.实现语言 C语言 B.环境要求 VC++ 6.0 n 2.  要求 请编写一个C语言程序.将若干学生编号按字母顺序(由小到大)输出. 程序的功能要求例如以下: 1)  输入password"admin",正确则进入主界面,错误则直接推出(exit(0)): 2)从键盘输入5个学生编号"BJS1001","BJS2001"."BJS1011&

关于JavaEE项目连接数据库提示 Access denied for user &#39;root&#39;@&#39;localhost&#39; (using password: YES)的解决方案

最近这三天,都在解决如下的问题:我MyEclipse中的JavaEE工程,运行之后就提示Access denied for user 'root'@'localhost' (using password: YES),当我用一个Java文件连接MySQL数据库时,没有任何问题.于是我在网上找了一些解决方案: (1)让root给当前用户授予增删改查的权限:grant select,insert,update,delete on *.* to 用户名@"%" Identified by &q

CentOS 7 / RHEL 7 : Reset / Recover forgotten root password

CentOS 7 / RHEL 7 : Reset / Recover forgotten root password October 11, 2014 by sharad chhetri 4 Comments In this post we will learn, how to reset / recover forgotten root password on CentOS 7 / RHEL 7 (Red Hat Enterprise Linux 7). On RHEL 5/6 or Cen

Reset root password in CentOS 7 / RHEL 7

There may be some occasion you will end up with requirement to reset the root password, the occasion comes when you forget root password; follow this guide to reset the password of root. In CentOS, single user mode will help us to achieve our goal of

How To Reset Your Forgotten Root Password On CentOS 7 Servers

Sometimes you forget stuff. I do. I forget important passwords for important websites sometimes. Retrieving your forgotten passwords for most websites is easy, all one has to do remember few details that were used when signing up for the service to g

#1045 - Access denied for user &#39;root&#39;@&#39;localhost&#39; (using password: NO)

如上所示,当我们修改数据库的密码之后,再次进入phpmyadmin的时候,就会产生这种错误#1045 - Access denied for user 'root'@'localhost' (using password: NO) .那这种错误产生的原因是什么呢?解释就是,密码修改后,新旧密码不一致产生的.改正过来其实也很简单,在phpmyadmin3.4.10.1文件夹下找到config.inc.php这个文件,然后找到里面$cfg['Servers'][$i]['password'] = '