A. Many Equal Substrings
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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
3 4
aba
output
Copy
ababababa
input
Copy
3 2
cat
output
Copy
catcat
这个题目虽然简单,但是揭示了kmp求next数组的原理
#include<iostream>
#include<cstring>
using namespace std;
int main(){
int n,k;
char s[101];
cin>>n>>k;
cin>>s;
int d=strlen(s);
// if(d==1){
// cout<<s<<endl;
// }
// else{
int len;
for(int j=1,i=d-2;i>=0&&j<=d-1;j++,i--){
for(int l=0,r=j;l<=i,r<d;l++,r++){
if(s[l]!=s[r]){
goto k;
}
}
len=i+1;
break;
k:;
}
// if(len>d/2){
// cout<<s<<endl;
// }
// else{
// cout<<len<<endl;
if(len==0){
for(int i=0;i<k;i++){
cout<<s;
}
cout<<endl;
}
else{
cout<<s;
for(int i=0;i<k-1;i++){
for(int j=len;j<d;j++){
cout<<s[j];
}
}
cout<<endl;
}
// }
// }
}
本文探讨了一个有趣的字符串构造问题,目标是构建最短的字符串,使其包含特定子串恰好k次。通过分析输入字符串的周期性,可以高效地解决此问题。示例代码展示了如何根据子串的重复模式来构建所需的字符串。
5748

被折叠的 条评论
为什么被折叠?



