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. position(static-relative-absolute-fixed),margin(top-right-bottom-left),top-right-bottom-left

    最近写css遇到一些问题,所以准备写下来捋一下思路. 1.position=satic下看margin的使用.(top-right-bottom-left在这种case下无效) 1-1)margin ...

  2. 数组&对象

    一.遍历数组的几种方式   var arr = [1,2,3]; Array.prototype.test=function(){} arr.name='jq'   1. for  /* * inde ...

  3. 设计模式六大原则(五):迪米特法则(Law Of Demeter)

    定义: 一个对象应该对其他对象保持最少的了解. 问题由来: 类与类之间的关系越密切,耦合度越大,当一个类发生改变时,对另一个类的影响也越大. 解决方案: 尽量降低类与类之间的耦合. PS: 自从我们接 ...

  4. 洛谷 P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib

    P1218 [USACO1.5]特殊的质数肋骨 Superprime Rib 题目描述 农民约翰的母牛总是产生最好的肋骨.你能通过农民约翰和美国农业部标记在每根肋骨上的数字认出它们.农民约翰确定他卖给 ...

  5. C 字符/字符串经常使用函数

    string.h中经常使用函数 char * strchr(char * str ,char ch); 从字符串str中查找首次出现字符ch的位置,若存在返回查找后的地址.若不存在则返回NULL vo ...

  6. 7. Spring Boot 启动加载数据 CommandLineRunner

    转自:https://blog.csdn.net/catoop/article/details/50501710

  7. python基础--数值类型和序列类型

    Python中数值类型:int(整数),float(浮点数),True/False(布尔值,首字母必须大写) int:1    #任意整数 float:2.3   #小数 python赋值: a = ...

  8. 基于mpvue的小程序项目搭建的步骤

    mpvue 是美团开源的一套语法与vue.js一致的.快速开发小程序的前端框架,按官网说可以达到小程序与H5界面使用一套代码.使用此框架,开发者将得到完整的 Vue.js 开发体验,同时为 H5 和小 ...

  9. 【CS Round #43 D】Bad Triplet

    [链接]点击打开链接 [题意] 给你n个点m条边的无权无向联通图; 让你找3个点A,B,C 使得A->B=B->C=A->C 这里X->Y表示点X到点Y的最短路长度. [题解] ...

  10. 使用javascript实现图片上下切换效果并且实现顺序循环播放

    <!doctype html><html lang="en"><head> <meta charset="UTF-8" ...