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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Light2Chasers/article/details/82033437
  1. problem link:http://codeforces.com/contest/1029/problem/A
  2. 题意:给你一个长度为n的字符串s和一个k,求出以s为子串且一共有k个s的最小长度的字符串并输出。
  3. 解题思路:倒序找出第一个与前面相等子串的位置,然后先输出原字符串再从此位置输出k-1次即可得到答案。一开始写了一个很长的代码,然后还错了。T_T。
    对了安利一个substr()函数。嗯哼~点这里有福利哟
  4. AC code:
#include<iostream>
using namespace std;
int main(){
    int n,k;string s;
    cin>>n>>k>>s;
    for(int i=n-1;i>0;i--)
        if(s.substr(n-i)==s.substr(0,i)){
            cout<<s;
            while(--k)cout<<s.substr(i);
            return 0;
        }
    while(k--)cout<<s;
}

猜你喜欢

转载自blog.csdn.net/Light2Chasers/article/details/82033437