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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • linux smartd failed

    linux smartd failed

    2021年8月6日
    54
  • Ffmpeg进行视频文件转换

    Ffmpeg进行视频文件转换

    2021年9月1日
    77
  • 更改pip源至国内镜像,显著提升下载速度

    更改pip源至国内镜像,显著提升下载速度经常在使用python的时候需要安装各种模块,而pip是很强大的模块安装工具,但是由于国外官方pypi经常被墙,导致不可用,所以我们最好是将自己使用的pip源更换一下,这样就能解决被墙导致的装不上库的烦恼。网上有很多可用的源,例如豆瓣:http://pypi.douban.com/simple/                     清华

    2022年6月4日
    54
  • POJ 2704 && HDU 1208 Pascal’s Travels (基础记忆化搜索)[通俗易懂]

    POJ 2704 && HDU 1208 Pascal’s Travels (基础记忆化搜索)[通俗易懂] Pascal’sTravelsTimeLimit:1000MS   MemoryLimit:65536K TotalSubmissions:5328   Accepted:2396  DescriptionAnnxngameboardispopulatedwithintegers,onenonnegative…

    2022年7月26日
    8
  • KRACK破解Wi-Fi WPA2加密协议 问题相关

    KRACK破解Wi-Fi WPA2加密协议 问题相关Wi-Fi爆重大安全漏洞,Android、iOS、Windows等所有无线设备都不安全了KRACK漏洞发现者回答纪实:所有设备都面临风险无线网络中所使用的WPA2安全加密协议遭到破解LineageOS已经着手修复WPA2安全加密协议的破解问题

    2022年5月1日
    37
  • 记一次遇到挖矿程序的经历「建议收藏」

    记一次遇到挖矿程序的经历「建议收藏」就在几天前,遇到了一次挖矿程序偷偷装在ECS阿里云服务器上的经历。那是一个风和日丽的上午,我和往常一样来到公司,倒杯水等待电脑打开,之后打开日常维护的几个系统。结果其中有一个OA系统,发现无法正常打开。一开始我以为是网络问题,但是发现打开其他网站正常,于是登上服务器准备探个究竟。登陆云服务器后,重启OA服务,发现报错,报错提示连接线程池连接不上。根据提示,怀疑是连不上部署在本地的数据服…

    2022年4月20日
    43

发表回复

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

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