【链接】 我是链接,点我呀:)

【题意】

给你一个字符串s
让你从中选出来一个字符串t
这个字符串t是s的前缀和后缀
且在除了前缀和后缀之外的中间部位出现过。
且要求t的长度最长。
让你输出这个字符串t

【题解】

KMP的应用
f[i]就是以i为结尾的后缀能匹配的最长前缀的长度
因此只要知道f[n]的值。
然后在做kmp的时候,看看中间有没有哪个时刻能匹配到长度为f[n]的前缀就好(开个数组标记一下就好);
如果没有就让j = f[f[n]]
直到匹配不到为止.

【代码】

import java.io.*;
import java.util.*; public class Main { static int N = (int)1e6;
static InputReader in;
static PrintWriter out; public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
} static class Task{
public void solve(InputReader in,PrintWriter out) {
int [] f = new int[N+10];
int [] ok = new int[N+10];
int n;
String s = in.next();
n = s.length();
f[0] = 0;f[1] = 0;
for (int i = 1;i < n;i++) {
int j = f[i];
while (j>0 && s.charAt(i)!=s.charAt(j)) j = f[j];
if (s.charAt(i)==s.charAt(j)) {
f[i+1] = j+1;
if (i<n-1) {
ok[j+1] = 1;
}
}
else
f[i+1] = 0;
} int j = f[n];
int temp = 0;
while (j>0) {
if (ok[j]==1) {
temp = Math.max(temp, j);
}
j = f[j];
} if (s.charAt(n-1)==s.charAt(0) && ok[1]==1) {
temp = Math.max(1, temp);
}
if (temp==0) {
out.print("Just a legend");
}else {
for (int i = 0;i < temp;i++) {
out.print(s.charAt(i));
}
}
}
} static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer; public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
} public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
} public int nextInt() {
return Integer.parseInt(next());
}
}
}

【Codeforces 126B】Password的更多相关文章

  1. 【Codeforces 411A】Password Check

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 傻逼模拟题 [代码] import java.io.*; import java.util.*; public class Main { st ...

  2. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  3. 【牛刀小试2】password保

    ]password保 主要知识: 1.        while循环 2.        do-while循环 3.        if-else 4.        strcmp()函数 [充电一下 ...

  4. 【codeforces 761C】Dasha and Password(动态规划做法)

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 【codeforces 761C】Dasha and Password(贪心+枚举做法)

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. 【codeforces 508D】Tanya and Password

    [题目链接]:http://codeforces.com/problemset/problem/508/D [题意] 给你一个字符的所有连续3个的子串; 让你复原出原串; (包含小写.大写字母以及数字 ...

  7. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  8. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  9. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

随机推荐

  1. ASP.NET Web Projects

    https://msdn.microsoft.com/en-us/library/ywdtth2f.aspx The topics in this section describe how to cr ...

  2. YTU 2776: 小壮的习惯

    2776: 小壮的习惯 时间限制: 1 Sec  内存限制: 128 MB 提交: 206  解决: 40 题目描述 小壮是一个英语初学者,一天,逗比的小壮看英语书,被其中的句子吸引了.他有个习惯,他 ...

  3. (多项式)因式分解定理(Factor theorem)与多项式剩余定理(Polynomial remainder theorem)(多项式长除法)

    (多项式的)因式分解定理(factor theorem)是多项式剩余定理的特殊情况,也就是余项为 0 的情形. 0. 多项式长除法(Polynomial long division) Polynomi ...

  4. 【POJ 2777】 Count Color

    [题目链接] http://poj.org/problem?id=2777 [算法] 线段树 [代码] #include <algorithm> #include <bitset&g ...

  5. codevs3981动态最大子段和(线段树)

    3981 动态最大子段和  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond     题目描述 Description 题目还是简单一点好... 有n个数,a ...

  6. codevs1154能量项链(环形dp,区间dp)

    1154 能量项链 2006年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold     题目描述 Description 在Mars星球上 ...

  7. akka设计模式系列(Actor模型)

    谈到Akka就必须介绍Actor并发模型,而谈到Actor就必须看一篇叫做<A Universal Modular Actor Formalism for Artificial Intellig ...

  8. Codeforces 769D

    太久没写搜索因为递归边界问题卡了很久.. 题意:定义k-interesting:如果两个数的二进制形式有k位不相同,则称之为k-interesting.给出n和k,输入n个大小在[0,10000]之间 ...

  9. Spring思维课程导图——bean得实例化和bean的管理

  10. vs项目结构解析

    当我们用VS开发一个项目的时候,首先应该清楚用VS这个IDE生成的一些文件和文件夹是什么意思,起什么作用,什么场合下使用. 因为我使用的是VS2015,就以这个为例来进行一些说明: 首先要做的是更改你 ...