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

【题意】

给你一个字符串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. 利用POI抽取word中的图片并保存在文件中

    利用POI抽取word中的图片并保存在文件中 poi.apache.org/hwpf/quick-guide.html 1.抽取word doc中的图片 package parse; import j ...

  2. Nginx网站用Let’sEncrypt证书开HTTPS

  3. PCB MS SQL 标量函数(CLR) 实现DataTable转HTML的方法

    一.准备需转为HMLT字符串的DataTable数据 在数据库中执行一段SQL返回的数据 需转换后的HTML的文本 <html ><head></head>< ...

  4. B. Trees in a Row(cf)

    B. Trees in a Row time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  5. mysql 数据去重

    update ptop_investrecord set delflag = 1 where cid  = 250 and uid = 92569  and delflag = 0 and progr ...

  6. HDU 1754 Java

    退役后学java... 裸线段树 //By SiriusRen import java.util.*; import java.math.*; public class Main{ public st ...

  7. BZOJ 4828 DP+BFS

    被一道简单BFS坑了这么长时间我也是hhh了 //By SiriusRen #include <bits/stdc++.h> using namespace std; ,,):d(D),x ...

  8. 上传Android代码到gerrit服务器

    1. 配置default.xml default.xml是跟Android代码配套的,可参考google Android源码下的default.xml(.repo/manifests/default. ...

  9. 使用HBuilder新建项目

    依次点击文件→新建→选择Web项目(按下Ctrl+N,W可以触发快速新建(MacOS请使用Command+N,然后左键点击Web项目)) 如上图,请在A处填写新建项目的名称,B处填写(或选择)项目保存 ...

  10. 从源码看ASP.NET框架(一)-打造页面控件树

    测试实例如下: 前台代码MyFirstWeb.aspx(没有服务器控件,即没有runat) CodeBehind="MyFirstWeb.aspx.cs":表示代码后置类文件 In ...