Kolya and Tandem Repeat
Kolya and Tandem Repeat
2 seconds
256 megabytes
standard input
standard output
Kolya got string s for his birthday, the string consists of small English letters. He immediately added
k more characters to the right of the string.
Then Borya came and said that the new string contained a
tandem repeat of length l as a substring. How large could
l be?
See notes for definition of a tandem repeat.
The first line contains s (1 ≤ |s| ≤ 200). This string contains only small English letters. The second line contains number
k (1 ≤ k ≤ 200) — the number of the added characters.
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
aaba
2
6
aaabbbb
2
6
abracadabra
10
20
A tandem repeat of length 2n is string
s, where for any position i (1 ≤ i ≤ n) the following condition fulfills:
si = si + n.
In the first sample Kolya could obtain a string
aabaab, in the second — aaabbbbbb, in the third —
abracadabrabracadabra
题意:给定一个字符串和一个数,k为可添加的字符数,求反复两次的最大字符串长度。
思路:暴力也能过,也可用hash进行优化。搜索,假设都在给定字符串内,推断同样,假设后面前短点在给定字符串里面,推断此端点到字符串结尾与前面相应的是否匹配。假设反复的那个都在补的里面。则不用推断。
AC代码:
import java.util.*;
public class Main {
static int hash[]=new int[210];
static int f[]=new int[210];
static boolean check(int x1,int y1,int x2,int y2){
return hash[y1]-hash[x1-1]*f[y1-x1+1]==hash[y2]-hash[x2-1]*f[y2-x2+1];
}
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
String s=scan.nextLine();
int k=scan.nextInt();
f[0]=1;
char a[]=new char[s.length()];
a=s.toCharArray();
int len=s.length();
for(int i=1;i<=len;i++){
hash[i]=hash[i-1]*111111+a[i-1];
f[i]=f[i-1]*111111;
}
int ans=0;
for(int i=1;i<=len+k;i++){
for(int j=i;j<=len+k;j++){
int x1=i,y1=j,x2=j+1,y2=j+j-i+1;
if(y2>len+k) break;
if(y2<=len){
if(check(x1,y1,x2,y2))
ans=Math.max(ans, y2-x1+1);
}
else if(x2<=len){
if(check(x1,x1+len-x2,x2,len))
ans=Math.max(ans, y2-x1+1);
}
else
ans=Math.max(ans,y2-x1+1);
}
}
System.out.println(ans);
} }
Kolya and Tandem Repeat的更多相关文章
- cf443B Kolya and Tandem Repeat
B. Kolya and Tandem Repeat time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- Codeforces 443 B. Kolya and Tandem Repeat
纯粹练JAVA.... B. Kolya and Tandem Repeat time limit per test 2 seconds memory limit per test 256 megab ...
- CF B. Kolya and Tandem Repeat
Kolya got string s for his birthday, the string consists of small English letters. He immediately ad ...
- codeforces 443 B. Kolya and Tandem Repeat 解题报告
题目链接:http://codeforces.com/contest/443/problem/B 题目意思:给出一个只有小写字母的字符串s(假设长度为len),在其后可以添加 k 个长度的字符,形成一 ...
- Codeforces Round #253 (Div. 2) B - Kolya and Tandem Repeat
本题要考虑字符串本身就存在tandem, 如测试用例 aaaaaaaaabbb 3 输出结果应该是8而不是6,因为字符串本身的tanderm时最长的 故要考虑字符串本身的最大的tanderm和添加k个 ...
- Codeforces 443 B Kolya and Tandem Repeat【暴力】
题意:给出一个字符串,给出k,可以向该字符串尾部添加k个字符串,求最长的连续重复两次的子串 没有想出来= =不知道最后添加的那k个字符应该怎么处理 后来看了题解,可以先把这k个字符填成'*',再暴力枚 ...
- CodeForces 443B Kolya and Tandem Repeat
题目:Click here 题意:给定一个字符串(只包含小写字母,并且最长200)和一个n(表示可以在给定字符串后面任意加n(<=200)个字符).问最长的一条子串长度,子串满足前半等于后半. ...
- Codeforces Round 253 (Div. 2)
layout: post title: Codeforces Round 253 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- 33、VCF格式
转载:http://blog.sina.com.cn/s/blog_7110867f0101njf5.html http://www.cnblogs.com/liuhui0622/p/6246111. ...
随机推荐
- MSSQL相关用法
一.分页查询 方式一(row_number): SELECT TOP pageSize * FROM (SELECT row_number() OVER (ORDER BY orderColumn) ...
- python ATM机 案例代码
利用目前学的流程控制写的 ''' ATM机 需求: 1.登陆 输入账号输入密码 每日只有3次登陆密码错误的机会,超过3次禁止登陆 2.查询余额 3.存款 4.取款 5.转帐 6.退出 ''' info ...
- vim学习3
可视模式:
- 设计模式六大原则(六): 开闭原则(Open Closed Principle)
定义: 一个软件实体如类.模块和函数应该对扩展开放,对修改关闭. 问题由来: 在软件的生命周期内,因为变化.升级和维护等原因需要对软件原有代码进行修改时,可能会给旧代码中引入错误,也可能会使我们不得不 ...
- IntelliJ IDEA 2018 Community(社区版)创建J2EE项目+Tomcat9部署
博主打算开始系统地自学JAVA,首要问题就是解决IDE的问题, 以前用过像VS.Android Studio.Eclipse,知道Eclipse是JAVA最传统的IDE, 用过VS和AS的朋友都知道, ...
- body{display:none}
body{display:none} 使浏览器不显示内容,用这样的代码删除 $document = str_replace('body{display:none}','',$document);
- 7. 基于Express实现接口
安装Mongoose 创建model //server/models/goods.js var mongoose = require('mongoose');//优先到node_modeles里加载 ...
- 通过wmi获取本地硬件信息的一些疑问。
通过wmi获取本地硬件信息的一些疑问. http://bbs.csdn.net/topics/391017789 http://blog.csdn.net/xcntime/article/detail ...
- java方法调用之动态调用多态(重写override)的实现原理——方法表(三)
上两篇篇博文讨论了java的重载(overload)与重写(override).静态分派与动态分派.这篇博文讨论下动态分派的实现方法,即多态override的实现原理. java方法调用之重载.重写的 ...
- DOM相关知识总结
DOM相关: 1.获取DOM元素 document.getElementById document.getElementsByName document.getElementsByTagName do ...