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. CSS min-height不能解决垂直外边距合并问题

    垂直外边距合并有一种情况是嵌套元素的垂直外边距合并,当父级元素没有设定外边距时,在顶部或者底部边缘的子元素的垂直外边距就会和父级的合并,导致父级也有了“隐形”的垂直外边距. 当父级元素的min-hei ...

  2. Spider_req

    requests模块 安装(用管理员身份去打开Anaconda Prompt) conda install requests python -m pip install requests # 以管理员 ...

  3. js课程 6-15 js简单弹力球如何实现

    js课程 6-15 js简单弹力球如何实现 一.总结 一句话总结:a.通过document的documentElement属性获取可是区域的高: b.通过增值变为负的实现到底部后反弹 1.docume ...

  4. js进阶 14 jquery的ajax有哪些函数和事件(多练)

    js进阶 14 jquery的ajax有哪些函数和事件(多练) 一.总结 一句话总结:常用:load.ajax.post.get.getScript().getJSON().表单序列化,ajax事件这 ...

  5. PHP中 “ . ” 和 “ ,”的区别

    在PHP中,“ . ”可以串接两个变量.而“ , ”却没什么用处.

  6. JavaScript 倒计时器,闹钟功能

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 理解spring对事务的处理:传播性

    所谓事务传播行为就是多个事务方法相互调用时,事务如何在这些方法间传播.Spring 支持 7 种事务传播行为: PROPAGATION_REQUIRED 如果当前没有事务,就新建一个事务,如果已经存在 ...

  8. 软件——机器学习与Python,输入输出的用法

    转自:http://www.cnblogs.com/graceting/p/3875438.html 输入很简单 x = input("Please input x:") Plea ...

  9. UVA 10125 - Sumsets(POJ 2549) hash

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  10. tomcat 服务形式检测

    http://blog.chinaunix.net/uid-20449851-id-2369842.html