【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子列的个数; 然后让你输出 ...
随机推荐
- xshell暂停串口的打印【转】
本文转载自:http://www.360doc.com/content/16/0311/10/7821691_541261680.shtml xshell不想CRT可以断开而停止打印,看这停下的打印信 ...
- ios集合
Foundation framework中用于收集cocoa对象(NSObject对象)的三种集合分别是: NSArray 用于对象有序集合(数组) NSSet 用于对象无序集合 (集合) NSDic ...
- PCB 挺有意思的基数排序----C#代码实现
今天在头条看一个很有意思的排序算法[基数排序],以前所学习的排序算法都是基于数值对比的方式排序的,而这个算法挺有意思的非常独特.但从网上看到的例子通常是对个位,十位处理,并转为对应的桶索引的方式实现, ...
- PCB Genesis 外形加内角孔实现方法
在PCB工程制作CAM时,经常会遇到外形拐角处直角的,而客户对内角是要求,比如最大内角要求R0.5mm或者不接受内角, 但成型方式为铣方式,又不是啤板成型,那怎么处理才可以达到要求效果呢,在这里介绍2 ...
- php字符串读取函数
function cc_msubstr($str, $length, $start=0, $charset="utf-8", $suffix=true){ if(function_ ...
- Unity项目 - 捡苹果 Apple Picker
项目展示 Github项目地址:Apple Picker 涉及知识 正投视图 3D场景内树与苹果的图层 记录最高分到本地 准备工作 模型制作: 基本模型创建 树叶:sphere 拉伸为椭圆形,绿色材质 ...
- curl 采集的时候遇到60报错怎么办?
1.到https://curl.haxx.se/ca/cacert.pem复制下文本粘贴到文件夹cart.pem 然后把catr.pem放到PHP的bin目录下 2.在php.ini中修改下面这句话, ...
- 解决macOS升级之后每次使用ssh都要输入密码的问题
最近想趁着假期把跟了我2年mac的系统重做下.于是就开始行动了,经过大半天的数据备份.然后进行了全盘格式化,使用了在线更新的方式从新安装升级到了10.12.6.这里提醒下有类似的想法的同学可以采用 ...
- CSS-类和ID选择器的区别
学习了类选择器和ID选择器,我们会发现他们之间有很多的相似处,是不是两者可以通用呢?我们不要着急先来总结一下他们的相同点和不同点: 相同点:可以应用于任何元素不同点: 1.ID选择器只能在文档中使用一 ...
- Unity Toast插件(UGUI版)
简介 介于自己之前经历的一些开发过程,非常希望unity能有类似Toast的功能用于一些简单的信息显示.但是找来找去找了半天,都木有发现,实在是憋不住了,自己写了个,感觉还可以用,发出来共享一下... ...