CF#506(div.3) A. 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 t

and 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

Copy

3 4
aba

Output

Copy

ababababa

Input

Copy

3 2
cat

Output

Copy

catcat

题意:给你一个长度为n得串 让你输出一个串 里面有 k个字串n

题解:CF上面的题好像时间卡的非常紧 。。每次都RE 。我们这里用next数组看他是否有循环饥节 然后可以推出上的代码结论(不太好说,不过你很容易能懂)

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
string s;
int nexxt[550];
int n,m;
void getnext()
{
	int i=0,j=-1;
	nexxt[0]=-1;
	while(i<n)
	{
		if(j==-1||s[i]==s[j])
		{
			i++,j++;
			nexxt[i]=j;
		}
		else
		j=nexxt[j];
	}
}
int main()
{
	ios::sync_with_stdio(false); 
	cin>>n>>m;
	cin>>s;
	getnext();
	cout<<s;
	for(int i=1;i<m;i++)
	{
		for(int j=nexxt[n];j<n;j++)
		{
			cout<<s[j];
		}
	}
	cout<<endl;
}

猜你喜欢

转载自blog.csdn.net/henucm/article/details/82081293