A. Many Equal Substrings(substr() 方法)

13 篇文章 0 订阅

滴答滴答---题目链接

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() 方法

JavaScript String 对象

定义和用法

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()

如何使用 substr() 从字符串提取一些字符。

JavaScript String 对象

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值