A. Many Equal Substrings 知识

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sdz20172133/article/details/82430138

      A. Many Equal Substrings

You are given a string tt consisting of nn lowercase Latin letters and an integer number kk .

Let's define a substring of some string ss with indices from ll to rr as s[l…r]s[l…r] .

Your task is to construct such string ss of minimum possible length that there are exactly kk positions ii such that s[i…i+n−1]=ts[i…i+n−1]=t . In other words, your task is to construct such string ss of minimum possible length that there are exactly kk substrings of ss equal to tt .

It is guaranteed that the answer is always unique.

Input

The first line of the input contains two integers nn and kk (1≤n,k≤501≤n,k≤50 ) — the length of the string tt and the number of substrings.

The second line of the input contains the string tt consisting of exactly nn lowercase Latin letters.

Output

Print such string ss of minimum possible length that there are exactly kk substrings of ss equal to tt .

It is guaranteed that the answer is always unique.

Examples

Input

Copy

 
  1. 3 4

  2. aba

Output

Copy

ababababa

Input

Copy

 
  1. 3 2

  2. cat

Output

Copy

catcat

题意:求一串字符,要求输出m个字符串,但是前缀和后缀可以重合,时期最短。

分析:以前看过字符串的匹配KMP,但是没看懂就敲了一遍模板,生搬硬套接近四五十分钟才出来,看代网上代码substr就出来了,真是,哎,学的东西太少了。

#include<cstdio>  
#include<cstring>  
#include<cstdlib>  
#include<cctype>  
#include<cmath>  
#include<iostream>  
#include<sstream>  
#include<iterator>  
#include<algorithm>  
#include<string>  
#include<vector>  
#include<set>  
#include<map>  
#include<stack>  
#include<deque>  
#include<queue>  
#include<list>  
using namespace std;  
const double eps = 1e-8;  
typedef long long LL;  
typedef unsigned long long ULL;  
const int INF = 0x3f3f3f3f;  
const int INT_M_INF = 0x7f7f7f7f;  
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;  
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;  
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};  
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};  
const int MOD = 1e9 + 7;  
const double pi = acos(-1.0);  
const int MAXN=40010;  
const int MAXM=100010;
const int M=500;
int n,m;
int main()
{
	int n,m;
	string s;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
    	cin>>s;
    	int len=s.length();
    	int  k=0;
    	int b[100];
    	memset(b,0,sizeof(b));
		for(int i=1;i<len;i++)
		{
		while(s[i]!=s[k]&&k!=0)
			k=b[k];
		  if(s[k]==s[i])	
		  {
		  	k++;
		  	
		  }b[i+1]=k;
		}
		cout<<s;
       for(int i=2;i<=m;i++)
       	for(int j=b[len];j<len;j++)
	   {
	   	  cout<<s[j];
	   }
	   cout<<endl;
    }
    return 0;
}
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<vector>
 
using namespace std;
 
int main()
{
	int n, m;
	string s;
	cin >> n >> m >> s;
	
	int temp;
	for(int i = 0; i < n; i++) if(s.substr(0, i) == s.substr(n-i, i)) temp = i;
	
	for(int i = 1; i < m; i++) cout << s.substr(0, n - temp);
	cout << s << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/sdz20172133/article/details/82430138