CF B. Kolya and Tandem Repeat

CF B. Kolya and Tandem Repeat

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

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.

暴力枚举全部情况

#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.net/115383.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 图像去噪序列——BM3D图像去噪模型实现

    图像去噪序列——BM3D图像去噪模型实现1.BM3D模型简介BM3D模型是一个两阶段图像去噪方法,主要包含两个步骤:(1)在噪声图像上,利用局部区域搜索相似块,并进行堆叠,在变换域(DCT域、FFT域)利用硬阈值去噪方法对堆叠的图像块进行去噪,获得堆叠相似块的估计值,最后,根据均值权重进行聚合;(2)通过步骤(1)获取初步估计的图像,在初步估计的图像上进行相似块的聚合;然后,利用维纳协同滤波进行图像去噪,从而,获取最后的去…

    2022年6月4日
    29
  • Clion2022.01激活码_在线激活

    (Clion2022.01激活码)好多小伙伴总是说激活码老是失效,太麻烦,关注/收藏全栈君太难教程,2021永久激活的方法等着你。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html40ZKSWCX8G-eyJsaWNlbnNlSWQi…

    2022年4月2日
    113
  • jquery怎么写ajax_js中ajax写法

    jquery怎么写ajax_js中ajax写法一、第一种:$.post("url",{"func":"getNameAndTime"},function(data,status){ },"json");二、第二种$.ajax({type:"GET",url:"",data:{},

    2022年9月30日
    4
  • pygame安装(超级详细)

    pygame安装(超级详细)安装时是需要设置python环境变量的,下载python的时候底下有个小框框(没有默认选中)AddPython3.7toPATH需要选择的,如果没有选择的话,需要自己设置,我一般比较懒,卸载了python重新下载的,让下载器自动设置。然后是python版本问题有人疑问这是64位还是32位,看那个[64bit]是64位,至于后面的on32是在windows上使用的意思,在Linux上…

    2022年5月24日
    202
  • 详解java接口interface

    详解java接口interface引言接口这个词在生活中我们并不陌生。在中国大陆,我们可以将自己的家用电器的插头插到符合它插口的插座上;我们在戴尔,惠普,联想,苹果等品牌电脑之间传输数据时,可以使用U盘进行传输。插座的普适性是

    2022年7月2日
    24
  • ostream iterator

    ostream iterator1.ostream_iteratortemplate         class_CharT=char,class_Traits=char_traits>classostream_iterator{public: typedef_CharT                        char_type; typedef_Traits

    2025年6月10日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号