cf 1029 A. Many Equal Substrings(字符串操作)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_41117236/article/details/82052885

【题目】

A. Many Equal Substrings

time limit per test 1 second

memory limit per test 256 megabytes

You are given a string t consisting of n lowercase Latin letters and an integer number k.

Let's define a substring of some string s with indices from l to r as s[l…r].

Your task is to construct such string s of minimum possible length that there are exactly k positions i such that s[i…i+n−1]=t. In other words, your task is to construct such string s of minimum possible length that there are exactly k substrings of s equal to t.

It is guaranteed that the answer is always unique.

Input

The first line of the input contains two integers n and k (1≤n,k≤501≤n,k≤50) — the length of the string t and the number of substrings.

The second line of the input contains the string t consisting of exactly nn lowercase Latin letters.

Output

Print such string ss of minimum possible length that there are exactly k substrings of s 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

【题解】

题意:构造一个最短字符串使其出现k次长度为n的给定字符串。

思路:找到给定字符串的最长前后缀,输出时可省去相同的前缀。

【代码】

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <stack>
#include <queue>
#include <algorithm>
#include <iostream>
#define mem(a) memset(a,0,sizeof(a))
#define go(i,a,b) for(int i=a;i<=b;i++)
#define og(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int maxn=1e5+5;
const int inf=0x3f3f3f3f;
typedef long long ll;
typedef unsigned long long ull;
main()
{
    int n,k; string a;
    cin>>n>>k; cin>>a;
    int l=n-1;
    string t1=a.substr(0,l);
    string t2=a.substr(n-l,l);
    while(t1!=t2&&l>0)
    {
        l--;
        t1=a.substr(0,l);
        t2=a.substr(n-l,l);
    }
    cout<<a;
    go(i,1,k-1)
    {
        go(j,l,n-1) cout<<a[j];
    }
    puts("");
}

猜你喜欢

转载自blog.csdn.net/qq_41117236/article/details/82052885