Codeforces Round #506 (Div. 3) A. Many Equal Substrings

 

 题意:构建字符串,即复制字符串s,k次,如果后缀与前缀重复省略前缀

题解:kmp板子next数组应用

#include<bits/stdc++.h>
using namespace std;
string s;
int nex[1000];
int n,k;
void  next_l()
{
    int len=s.size();
    nex[0]=-1;
    int i=0,j=-1;
    while(i<len)
    {
        if(j==-1||s[i]==s[j])
        {
            i++;
            j++;
            nex[i]=j;
        }
        else
            j=nex[j];
    }
}
int main()
{
 
    int n,k;
    cin>>n>>k;
    cin>>s;
   cout<<s;
    next_l();
    k-=1;
 
    while(k--)
    {
        for(int i=nex[n]; i<n; i++)
            cout<<s[i];
    }
 
}

 

posted @ 2020-02-12 22:21  RE-TLE  阅读(107)  评论(0编辑  收藏  举报