【题解】codeforces1029A[Codeforces Round #506 (Div. 3)]A.Many Equal Substrings KMP

32 篇文章 0 订阅
1 篇文章 0 订阅

题目链接

Description

You are given a string t t t consisting of n n n lowercase Latin letters and an integer number k k k.

Let’s define a substring of some string s s s with indices from l l l to r r r as s [ l … r ] s[l…r] s[lr].

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

It is guaranteed that the answer is always unique.

Input

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

The second line of the input contains the string t t t consisting of exactly n n n lowercase Latin letters.

Output

Print such string s s s of minimum possible length that there are exactly k k k substrings of s s s equal to t t t.

It is guaranteed that the answer is always unique.

Examples

jnput

3 4
aba

Output

ababababa

Input

3 2
cat

Output

catcat


类似 K M P KMP KMP 算法,我们求出一个最长的相同前缀后缀长度,即 n e x t next next 数组,然后输出 k − 1 k-1 k1 S [ i ] , i ∈ [ 0 , s . s i z e ( ) − n e x t [ n − 1 ] ] S[i],i∈\big[0,s.size()-next[n-1]\big] S[i],i[0,s.size()next[n1]],最后再输出 S S S

#include<cstdio>
#include<iostream>
using namespace std;
int nx[110];
int main()
{
	//freopen("in.txt","r",stdin);
	int n,k;
	string s;
	cin>>n>>k>>s;
	for(int i=1,j=0;i<s.size();i++)
	{
		while(j&&s[i]!=s[j])j=nx[j-1];
		if(s[i]==s[j])j++;
		nx[i]=j;
	}
	int len=s.size()-nx[n-1];
	for(int i=1;i<k;i++)
	    cout<<s.substr(0,len);
	cout<<s;
	return 0;
}

总结

字符串算法没怎么写过题,很生疏。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值