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)
上一篇 2022年2月5日 上午8:00
下一篇 2022年2月5日 上午9:00


相关推荐

  • 腾讯混元HY-Motion 1.0保姆级教程:一键部署,用一句话生成丝滑3D动作

    腾讯混元HY-Motion 1.0保姆级教程:一键部署,用一句话生成丝滑3D动作

    2026年3月14日
    2
  • 个人微信api接口调用代码[通俗易懂]

    个人微信api接口调用代码[通俗易懂]个人微信api接口1、微信好友收发消息/***给微信好友发消息*@authorwechatno:tangjinjinwx*@bloghttp://www.wlkankan.cn*/@AsyncpublicvoidhandleMsg(ChannelHandlerContextctx,TransportMessagevo,StringcontentJsonStr){try{…

    2022年10月2日
    4
  • pytorch交叉熵损失函数计算_pytorch loss不下降

    pytorch交叉熵损失函数计算_pytorch loss不下降MSE:MeanSquaredError(均方误差)含义:均方误差,是预测值与真实值之差的平方和的平均值,即:MSE=1N∑i=1n(xi−yi)2\begin{aligned}MSE=\cfrac{1}{N}\sum_{i=1}^n(x_i-y_i)^2\end{aligned}MSE=N1​i=1∑n​(xi​−yi​)2​  但是,在具体的应用中跟定义稍有不同。主要差别是参数的设置,在torch.nn.MSELoss中有一个reduction参数。reduction是维度要不要

    2026年1月18日
    10
  • 银行机构代码_工商银行怎么查12位行号

    银行机构代码_工商银行怎么查12位行号因为做到绑定银行卡的时候,需要定义一下银行卡的代号。  找了一下这方面的资源: 银行机构代码  央行颁发支付系统银行行别、行号业务标准,支付系统银行行别代码采取类别编码方法,实行3位定长数字,由类别代码和顺序编码组成。其中第一位为类别代码,用于区分不同种类的银行机构,便于金融统计数据的提取;第二、三位为顺序编码,用于标识每一家银行机构。  银行行别代码结构:  一、类别代码…

    2025年8月7日
    5
  • vue关闭Eslint验证

    vue关闭Eslint验证在创建项目时 手贱点击了 yes 那么怎样关闭 Eslint 验证呢 1 打开 build webpack base conf js2 找到如下代码 并 ctrl 点击红框框起的代码 3 修改 useEslint true 为 false4 最后 在修改完配置文件之后 在终端输入 nmprundev 这一点非常重要 我就是没有漏了这步操作 弄了很久

    2026年3月18日
    3
  • Python字典查找报Keyerror解决方法

    Python字典查找报Keyerror解决方法Python 的字典一般都直接查找 key 比如 dict a 1 b 2 c 3 print dict a 但是如果在查找的 key 不存在的时候就会报 KeyError 比如你要查看 print dict d 由于这个时候 dict 里面并没有这个 key 所以就会直接报错 那么这个时候其实 python 给我们提供了一种很棒的解决方法 那就是用 setdefa

    2026年3月26日
    3

发表回复

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

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