Many Equal Substrings

                                 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 ttnnkk

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:

3 4

aba


output:

ababababa

input:

3 2

cat

output:

catcat

题意:

输出一个字符串使得给定长度为n的字符串在其中出现k次,求符合条件的长度最小的字符串,我们只要求出"循环节"即可,举个例子:给我们的字符串是"abcab",不要把该字符串的循环节看作1,应该是3,这就是这题构造字符串的核心,因为我们构造的时候会循环一部分字符串,接着上面的列子说:abc(abc)(abc)(abc)ab,我们输出k次循环节,然后输出原字符串除去循环节的部分,代码如下:

 for(int i=1; i<=n; i++)
        {
            int flag=1;
            for(int j=0; j+i<n; j++)
            {
                if(str[j]!=str[j+i])
                {
                    flag=0;
                    break;
                }
            }
            if(flag)
            {
                num=i;
                break;
            }
        }

 ac代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int n,k,num;
string str;
int main()
{
    while(scanf("%d %d",&n,&k)!=EOF)
    {
        cin>>str;
        for(int i=1; i<=n; i++)
        {
            int flag=1;
            for(int j=0; j+i<n; j++)
            {
                if(str[j]!=str[j+i])
                {
                    flag=0;
                    break;
                }
            }
            if(flag)
            {
                num=i;
                break;
            }
        }
        string str1=str.substr(0,num);
        string str2=str.substr(num);
        for(int i=1;i<=k;i++)
            cout<<str1;
        cout<<str2<<endl;
    }
    return 0;
}

 

 

 

 

 

全部评论

相关推荐

代码飞升_不回私信人...:别这样贬低自己,降低预期,放平心态,跟昨天的自己比。做好自己,反而会效率更高心态更好,加油兄弟
点赞 评论 收藏
分享
12-15 11:27
门头沟学院 Java
哇哇的菜鸡oc:所有人不要理会,就好了,后面他就知道怎么回事了,只能说有的时候市场都是被宰的人搞坏的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务