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
题意:构造一个最短字符串使其出现k次长度为n的给定字符串。
思路:找到给定字符串的最长前后缀,输出时可省去相同的前缀。
用到了substr(),看看他的用法吧,在下面呢,
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,m;
string s;
cin>>n>>m>>s;
int l;
for(int i=0; i<n; i++)
if(s.substr(0,i)==s.substr(n-i,i))
l=i;
for(int i=1; i<m; i++)
cout<<s.substr(0,n-l);
cout<<s<<endl;
return 0;
}
JavaScript substr() 方法
定义和用法
substr() 方法可在字符串中抽取从 start 下标开始的指定数目的字符。
语法
stringObject.substr(start,length)
参数 | 描述 |
---|---|
start | 必需。要抽取的子串的起始下标。必须是数值。如果是负数,那么该参数声明从字符串的尾部开始算起的位置。也就是说,-1 指字符串中最后一个字符,-2 指倒数第二个字符,以此类推。 |
length | 可选。子串中的字符数。必须是数值。如果省略了该参数,那么返回从 stringObject 的开始位置到结尾的字串。 |
返回值
一个新的字符串,包含从 stringObject 的 start(包括 start 所指的字符) 处开始的 length 个字符。如果没有指定 length,那么返回的字符串包含从 start 到 stringObject 的结尾的字符。
提示和注释
注释:substr() 的参数指定的是子串的开始位置和长度,因此它可以替代 substring() 和 slice() 来使用。
重要事项:ECMAscript 没有对该方法进行标准化,因此反对使用它。
重要事项:在 IE 4 中,参数 start 的值无效。在这个 BUG 中,start 规定的是第 0 个字符的位置。在之后的版本中,此 BUG 已被修正。
实例
例子 1
在本例中,我们将使用 substr() 从字符串中提取一些字符:
<script type="text/javascript">
var str="Hello world!"
document.write(str.substr(3)
)
</script>
输出:
lo world!
例子 2
在本例中,我们将使用 substr() 从字符串中提取一些字符:
<script type="text/javascript">
var str="Hello world!"
document.write(str.substr(3,7)
)
</script>
输出:
lo worl
TIY
如何使用 substr() 从字符串提取一些字符。