码迷,mamicode.com
首页 > 其他好文 > 详细

CQUOJ 10672 Kolya and Tandem Repeat

时间:2016-04-22 01:02:59      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

A. Kolya and Tandem Repeat

Time Limit: 2000ms
Memory Limit: 262144KB
64-bit integer IO format: %I64d      Java class name: Any

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 Input

Input
aaba
2
Output
6
Input
aaabbbb
2
Output
6
Input
abracadabra
10
Output
20

Hint

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.

 

 1 /*
 2 2016年4月22日00:01:53
 3 
 4 题意:给出一个字符串,给出k,可以向该字符串尾部添加k个字符串,求最长的连续重复两次的子串
 5 
 6     看了题解,可以先把这k个字符填成‘*‘,再暴力枚举起点和长度,找出最大的长度
 7 */
 8 
 9 # include <iostream>
10 # include <cstdio>
11 # include <cstring>
12 # include <algorithm>
13 # include <queue>
14 # include <vector>
15 # define INF 0x3f3f3f3f
16 using namespace std;
17 
18 int main(void)
19 {
20     char s[20005];
21     int n, len, i, j, k, flag, ans;
22     while (~scanf("%s %d", &s, &n)){
23         getchar();
24         len = strlen(s);
25         for (i = len; i < len+n; i++)
26             s[i] = *;
27         len += n;
28         ans = -1;
29         for (i = 0; i < len; i++){
30             for (j = 1; 2*j+i<=len; j++){
31                 flag = 1;
32                 for (k = i; k < i+j; k++){
33                     if (s[k]!=s[k+j] && s[k+j]!=*){
34                         flag = 0;
35                         break;
36                     }
37                 }
38                 if (flag) ans = max(ans, 2*j);
39             }
40         }    
41         printf("%d\n", ans);
42     }
43 
44     return 0;    
45 }

 

CQUOJ 10672 Kolya and Tandem Repeat

标签:

原文地址:http://www.cnblogs.com/hyq123456/p/5419512.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!