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.
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.
Print a single number — the maximum length of the tandem repeat that could have occurred in the new string.
aaba 2
6
aaabbbb 2
6
abracadabra 10
20
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
枚举吧,字符不长。
AC代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define M 100005
#define ll long long
using namespace std;
int main()
{
char a[10005];
int k;
cin>>a>>k;
int l=strlen(a);
int i,m,j,o,ans;
m=l+k;
m=m-m%2;
if(k>=l)
{cout<<m<<endl;return 0;}//特别记住这种情况
int max=0;
for(i=0;i<l;i++)//字符的起始
{
for(j=1;j<=l-i;j++)//枚举字符的长度
{
for(o=i,ans=0;o<i+j;o++)
{
if(o+j>=l&&o+j<l+k)
ans++;
else if(a[o]==a[o+j])
ans++;
}
if(ans==j&&2*ans>max)//验证符合否
max=2*ans;
}
}
cout<<max<<endl;
return 0;
}