D - Many Equal Substrings

You are given a string t consisting of n lowercase Latin letters and an integer number k
Let’s define a substring of some string s
with indices from l to r as s[l…r]Your task is to construct such string s
of minimum possible length that there are exactly k positions i such that s[i…i+n−1]=t. In other words, your task is to construct such string s of minimum possible length that there are exactly k substrings of s equal to t It is guaranteed that the answer is always unique.
Input
The first line of the input contains two integers n
and k (1≤n,k≤50) — the length of the string tand the number of substrings.The second line of the input contains the string t
consisting of exactly n lowercase Latin letters.
Output
Print such string s
of minimum possible length that there are exactly k substrings of s equal to t It is guaranteed that the answer is always unique.Examples

Input
3 4
aba
Output
ababababa

Input
3 2
cat
Output
catcat

BZ:我一开始其实是没读题目的,直接看样例我就知道了。(其实最大就50,全列出来都AC,哪里会T,我想多了。。我们想到了几乎所有的情况,就差一种,暴力就过了,瞎想啥。)后面上下补题后的代码。

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<queue>
#include<stdlib.h>
#include<cstring>
#include<string.h>
#include<string>
#include<math.h>
using namespace std ;
typedef long long ll;
#define MAXN 1005
#define INF 0x3f3f3f3f//将近int类型最大数的一半,而且乘2不会爆int

int main()
{
    int n, k;
    string s;
    cin >> n >> k >> s;
    int temp = 0;
    for(int i=0; i<n; ++i)
    {
        if(s.substr(0, i) == s.substr(n-i, i))// 找出前缀和后缀相同的最大长度 
            temp = i;
    }
        cout << s;
    for(int i=0; i<k-1; ++i)
        cout << s.substr(temp);
    cout << '\n';
    return 0;
}

猛然发现,原来string中有一个substr()函数,下面补充几点:(一个字符串截取函数。)

  1. 用途:一种构造string的方法

  2. 形式:s.substr(pos, n)

  3. 解释:返回一个string,包含s中从pos开始的n个字符的拷贝(pos的默认值是0,n的默认值是s.size() - pos,即不加参数会默认拷贝整个s)

  4. 补充:若pos的值超过了string的大小,则substr函数会抛出一个out_of_range异常;若pos+n的值超过了string的大小,则substr会调整n的值,只拷贝到string的末尾

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值