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()函数,下面补充几点:(一个字符串截取函数。)
-
用途:一种构造string的方法
-
形式:s.substr(pos, n)
-
解释:返回一个string,包含s中从pos开始的n个字符的拷贝(pos的默认值是0,n的默认值是s.size() - pos,即不加参数会默认拷贝整个s)
-
补充:若pos的值超过了string的大小,则substr函数会抛出一个out_of_range异常;若pos+n的值超过了string的大小,则substr会调整n的值,只拷贝到string的末尾