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
aaba2
Output
6
Input
aaabbbb2
Output
6
Input
abracadabra10
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 }