【Codeforces 126B】Password
【链接】 我是链接,点我呀:)
【题意】
给你一个字符串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的更多相关文章
- 【Codeforces 411A】Password Check
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 傻逼模拟题 [代码] import java.io.*; import java.util.*; public class Main { st ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【牛刀小试2】password保
]password保 主要知识: 1. while循环 2. do-while循环 3. if-else 4. strcmp()函数 [充电一下 ...
- 【codeforces 761C】Dasha and Password(动态规划做法)
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 761C】Dasha and Password(贪心+枚举做法)
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 508D】Tanya and Password
[题目链接]:http://codeforces.com/problemset/problem/508/D [题意] 给你一个字符的所有连续3个的子串; 让你复原出原串; (包含小写.大写字母以及数字 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
随机推荐
- WPF-使用面板控制内容布局,比较Canvas,WrapPanel,StackPanel,Grid,ScrollViewer
WPF-使用面板控制内容布局,比较Canvas,WrapPanel,StackPanel,Grid,ScrollViewer 分类: WPF2012-04-24 09:59 660人阅读 评论(0) ...
- 把ANSI格式的TXT文件批量转换成UTF-8文件类型
把ANSI格式的TXT文件批量转换成UTF-8文件类型 Posted on 2010-08-05 10:38 moss_tan_jun 阅读(3635) 评论(0) 编辑 收藏 #region 把AN ...
- JavaScript 中String和int互相转换
在javascript里怎么样才能把int型转换成string型 (1) var num = 0; a = x.toString(); (2) var x = 0; a = x + ...
- scrapy xpath中提取多个class值
xpath中没有提供对class的原生查找方法.但是 stackoverflow 看到了一个很有才的回答: This selector should work but will be more eff ...
- codevs3728联合权值(LCA)
3728 联合权值 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 输入描述 Input Des ...
- Akka源码分析-Remote-Actor创建
在之前的博客中,我们分析过local模式下Actor的创建过程,最终还是调用了provider的actorOf的函数创建了Actor,在remote模式下provider就是RemoteActorRe ...
- ACM_Cipher(异或运算)
Cipher Time Limit: 2000/1000ms (Java/Others) Problem Description: 只剩两个小时就要正式开始第一次月赛了,大四师兄决定还是来出一道,找点 ...
- myeclipse配置tomcat后,无法正常使用的问题
如图所示:一定要设置为Enable.否则部署tomcat时,没有tomcat8.0
- c++ 四种类型转换机制
类型转换机制可以分为:隐式类型转换 和 显示类型转换(强制类型转换) C中的类型转换: 事情要从头说起,这个头就是C语言.我们已经习惯了使用C-like类型转换,因为它强大而且简单. 主要有一下两种形 ...
- ios数据的基本类型和流程控制
swift的声明变量方式和js是类似的.基本类型基本都和java的差不多,多了字符类型. let:用于声明常量: var:用于声明变量: 基本类型有:double,float,Int(数字类型):bo ...