Kolya and Tandem Repeat

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.

Sample test(s)
Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20
Note

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的更多相关文章

  1. cf443B Kolya and Tandem Repeat

    B. Kolya and Tandem Repeat time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  2. 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 ...

  3. CF B. Kolya and Tandem Repeat

    Kolya got string s for his birthday, the string consists of small English letters. He immediately ad ...

  4. codeforces 443 B. Kolya and Tandem Repeat 解题报告

    题目链接:http://codeforces.com/contest/443/problem/B 题目意思:给出一个只有小写字母的字符串s(假设长度为len),在其后可以添加 k 个长度的字符,形成一 ...

  5. Codeforces Round #253 (Div. 2) B - Kolya and Tandem Repeat

    本题要考虑字符串本身就存在tandem, 如测试用例 aaaaaaaaabbb 3 输出结果应该是8而不是6,因为字符串本身的tanderm时最长的 故要考虑字符串本身的最大的tanderm和添加k个 ...

  6. Codeforces 443 B Kolya and Tandem Repeat【暴力】

    题意:给出一个字符串,给出k,可以向该字符串尾部添加k个字符串,求最长的连续重复两次的子串 没有想出来= =不知道最后添加的那k个字符应该怎么处理 后来看了题解,可以先把这k个字符填成'*',再暴力枚 ...

  7. CodeForces 443B Kolya and Tandem Repeat

    题目:Click here 题意:给定一个字符串(只包含小写字母,并且最长200)和一个n(表示可以在给定字符串后面任意加n(<=200)个字符).问最长的一条子串长度,子串满足前半等于后半. ...

  8. Codeforces Round 253 (Div. 2)

    layout: post title: Codeforces Round 253 (Div. 2) author: "luowentaoaa" catalog: true tags ...

  9. 33、VCF格式

    转载:http://blog.sina.com.cn/s/blog_7110867f0101njf5.html http://www.cnblogs.com/liuhui0622/p/6246111. ...

随机推荐

  1. jQ-多选按钮实现单选按钮的功能以及input按钮的优化

    css: .displayN{display: none;} label {font-size:12px;cursor:pointer;} label i {font-size:12px;font-s ...

  2. 关于Webpack详述系列文章 (第二篇)

    1.缩小文件搜索范围 1.1.1 include & exclude module:{ rules:[ { test:/\.js$/, use:['babel-loader?cacheDire ...

  3. Maven学习总结(14)——Maven 多模块项目如何分工?

    一.开场白 使用Maven有段时间了,只能感慨真是个好东西,让我从传统模式体会到了严谨.规范.敏捷.方便的特性. 如果你懂Maven或许看过Juven翻译的<Maven权威指南>: 发个牢 ...

  4. cx_Oracle

    cx_Oracle 安装 pip install cx_Oracle 只是我没用那个安装成功过.我找了rpm 包. http://nchc.dl.sourceforge.net/project/cx- ...

  5. 右键菜单→新建→BAT 批处理文件

    目的:以前编写BAT,通常新建一个文本,然后另存为 .bat,比较麻烦,那么如何右键新建菜单里添加新建批处理文件呢? 代码如下: @echo offcd /d %temp%echo Windows R ...

  6. 学习笔记:Vue——处理边界情况

    访问元素&组件 01.访问根实例 $root // Vue 根实例 new Vue({ data: { foo: 1 }, computed: { bar: function () { /* ...

  7. C#中对XML的操作

    现在有一个xml文件,名称:BookStore.xml,数据如下: <?xml version="1.0" encoding="gb2312"?>& ...

  8. COGS——C 14. [网络流24题] 搭配飞行员

    http://cogs.pro/cogs/problem/problem.php?pid=14 ★★☆   输入文件:flyer.in   输出文件:flyer.out   简单对比时间限制:1 s  ...

  9. Android Warning not all local changes may be shown due to an error

    idea使用svn出现Warning not all local changes may be shown due to an error,如下图所示: 解决方案: 1.File > Setti ...

  10. FZU《C语言程序综合设计》

    一年前的玩意. 老是有人找我要..一年前写得这么搓都不敢拿出来.... 但是好多人要啊.....直接发blog,省得下次还要发压缩文件.. 就不要吐槽我代码烂了,我也觉得很烂,至少现在看来确实很烂.. ...