codeforces Round 506(div 3) A. 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 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 kk 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 n lowercase Latin letters.

Output

Print 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.

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≤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 n lowercase Latin letters.

Output

Print 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.

Examples

input

3 4
aba

output

ababababa

input

3 2
cat

output

catcat

题意:给你一个字符串 t,和一个整数 k, 要你构造出一个最短的字符串  其中 t 出现的次数为 k

思路:这个题有很多细节值得注意, 首先是有循环节的情况  如  sprimsprimsprim  这样的,其次就是   aaaaa 这样的,然后有 abcab  这类的。如果使用 KMP求   出现次数 * 循环节长度  == 字符串 t 的长度 来判断 循环节的, 要注意还需要判断一次 ,如  xxxb,  xx 的出现次数是2 ,它的长度也为2, 2 * 2 == 4, 但他不是循环节。

AC代码:

#include<bits/stdc++.h>
using namespace std;

int nex[100];

void getnext(char *t,int lent){
    int i = 0,j = -1;
    nex[i] = -1;
    while(i < lent){
        if(j == -1 || t[i] == t[j])
            i ++,j ++, nex[i] = j;
        else j = nex[j];
    }
}

int kmp_count(char *t,char *s,int lent,int lens){
    int i = 0,j = 0;
    int ans = 0;
    memset(nex,0,sizeof(nex));
    getnext(t,lent);
    while(i < lens){
        if(j == -1 || s[i] == t[j])
            i ++,j ++;
        else j = nex[j];
        if(j == lent){
            i --; ans ++;
            j = nex[j - 1];
        }
    }
    return ans;
}

int main()
{
    int n,k;
    while(~scanf("%d%d",&n,&k)){
        char t[100]; scanf("%s",t);
        char tmp[100] = {0}; int ptm = 0;
        int len = strlen(t), tim;
        for(int i = 0;i < len;i ++){
            tmp[ptm ++] = t[i];
            tim = kmp_count(tmp,t,ptm,len);
            if(tim * ptm == len) break;
        }
        char a[100] = {0};
        for(int i = 0;i < tim;i ++) strcat(a,tmp);
        int flag = 0;
        if(kmp_count(tmp,a,ptm,ptm * tim) == tim && strcmp(tmp,t)) flag = 1;
        if(flag){                               ///有循环结
            printf("%s",t);
            for(int i = 0;i < k - 1;i ++) printf("%s",tmp);
        }
        else {
            char f[100] = {0};
            int p = len, wz = -1;
            for(int i = len - 1;i >= 0;i --){            /// 寻找能够重复使用的字符,如abcab 中的 ab
                f[-- p] = t[i];
                int vis = 1, coun = 0;
                for(int j = p;j < len;j ++)
                    if(f[j] != t[coun ++]){
                        vis = 0; break;
                    }
                if(vis == 1 && p != 0) wz = p;          /// p 等于0的话,意思就是寻到了本身,这里是针对单个字符的情况, 如样例  1  50  q   .
            } 
            if(wz == -1){
                for(int i = 0;i < k;i ++) printf("%s",t);
            }
            else {
                char xh[100] = {0}; int px = 0;
                for(int i = len - wz;i < len;i ++) xh[px ++] = t[i];    ///如 abcab ,之前求得的是 ab, 但实际需要的是  cab,  所以是从 len - wz 开始
                printf("%s",t);
                for(int i = 0;i < k - 1;i ++) printf("%s",xh);
            }
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/no_O_ac/article/details/82081061