CodeForces 443B Kolya and Tandem Repeat

本文介绍了一个编程问题的解决方法,即求解给定字符串在添加k个字符后可能产生的最大重复序列长度。通过直接枚举的方法,在限定的时间和空间复杂度内找到最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接:http://codeforces.com/problemset/problem/443/B

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个任意的字符。问你这个新串中包含的重复序列的最大长度为多大。重复序列是这样定义的:一个长度为2n的重复序列s满足si=si+n,1<=i<=n。


思路——因为字符长度最大为200,k最大也只为200,所以直接暴力枚举即可解决。首先可以特判一下字符串长度和k的关系,如果字符串长度小于等于k,那么直接输出该长度加上k除以二取整再乘以二即可。否则,从串首开始枚举。枚举的是重复序列一半的长度,当该长度满足重复序列的条件时,将它与当前最长长度比较,大的话,就将最大值改为现在的长度,否则不变。最后枚举完所有字符,输出那个最大值乘以二即可。


复杂度分析——时间复杂度:O(len^2),空间复杂度:O(len)


附上AC代码:


#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <map>
//#pragma comment(linker, "/STACK:102400000, 102400000")
using namespace std;
typedef unsigned int li;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const double pi = acos(-1.0);
const double e = exp(1.0);
const double eps = 1e-8;
const short maxlen = 205;
char str[maxlen];
short k;

int main()
{
	ios::sync_with_stdio(false);
	while (~scanf("%s%hd", str, &k))
	{
		short len = strlen(str);
		if (len <= k)
		{
			printf("%d\n", (len+k)/2*2);
			continue;
		}
		short maxnum = 0;
		for (short i=0; i<len; i++)
		{
			for (short j=1; j<=len-i; j++)
			{
				short cnt = 0;
				for (short s=i; s<i+j; s++)
				{
					if (s+j>=len && s+j<len+k)
						cnt++;
					else if (s+j<len && str[s]==str[s+j])
						cnt++;
				}
				if (j == cnt)
					maxnum = max(maxnum, cnt);
			}
		}
		printf("%d\n", maxnum*2);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值