前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CF B. Kolya and Tandem Repeat

CF B. Kolya and Tandem Repeat

作者头像
全栈程序员站长
发布2022-07-10 14:40:58
2450
发布2022-07-10 14:40:58
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是全栈君。

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.

暴力枚举全部情况

代码语言:javascript
复制
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;

char str[205];

int main(){
    int n;
    while(cin>>str>>n){
        int len = strlen(str);
        int L = len+n-(len+n)%2; //保证长度为偶数
        if(len <= n){
           cout<<L<<endl;
           continue;
        }
        int maxlen = 0;
        for(int i=0; i<len; i++){ //枚举起始位置
            for(int j=1; i+j-1<=len-1; j++){ //枚举一半的长度
                int cnt = 0;
            for(int k=i; k<=i+j-1; k++){ //推断
                if(len <= k+j && k+j < len+n) cnt++; //下标
                else if(str[k] == str[k+j]) cnt++;
            }
            if(cnt == j && 2*cnt > maxlen)
                maxlen = 2*cnt;
            }
        }
        cout<<maxlen<<endl;
    }
    return 0;
}

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/115383.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年2月5,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档