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;
}
// }
// }
}