CF1537E1 Erase and Extend (Easy Version)-Codeforces Round #726 (Div. 2

这篇博客讨论了一个编程问题,涉及到字符串操作和动态规划。问题要求找到通过删除和复制操作得到的长度为k的最小字典序字符串。作者指出,网上的一些解决方案可能不够严谨,并提供了两种不同的递归和非递归方法来解决这个问题。代码示例展示了如何使用深度优先搜索(DFS)和直接计算策略来找到答案。
摘要由CSDN通过智能技术生成

问题连接Problem - 1537E1 - Codeforces

E1. Erase and Extend (Easy Version)

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

This is the easy version of the problem. The only difference is the constraints on nn and kk. You can make hacks only if all versions of the problem are solved.

You have a string ss, and you can do two types of operations on it:

  • Delete the last character of the string.
  • Duplicate the string: s:=s+ss:=s+s, where ++ denotes concatenation.

You can use each operation any number of times (possibly none).

Your task is to find the lexicographically smallest string of length exactly kk that can be obtained by doing these operations on string ss.

A string aa is lexicographically smaller than a string bb if and only if one of the following holds:

  • aa is a prefix of bb, but a≠ba≠b;
  • In the first position where aa and bb differ, the string aa has a letter that appears earlier in the alphabet than the corresponding letter in bb.

Input

The first line contains two integers nn, kk (1≤n,k≤50001≤n,k≤5000) — the length of the original string ss and the length of the desired string.

The second line contains the string ss, consisting of nn lowercase English letters.

Output

Print the lexicographically smallest string of length kk that can be obtained by doing the operations on string ss.

Examples

input

Copy

8 16
dbcadabc

output

Copy

dbcadabcdbcadabc

input

Copy

4 5
abcd

output

Copy

aaaaa

Note

In the first test, it is optimal to make one duplication: "dbcadabc" →→ "dbcadabcdbcadabc".

In the second test it is optimal to delete the last 33 characters, then duplicate the string 33 times, then delete the last 33 characters to make the string have length kk.

"abcd" →→ "abc" →→ "ab" →→ "a" →→ "aa" →→ "aaaa" →→ "aaaaaaaa" →→ "aaaaaaa" →→ "aaaaaa" →→ "aaaaa".

-----------------------------------------------------------------------------------------------------------------------

网上很多论证,存在严重缺陷,本题可以说不应该是一个凭借感觉得出来的题目,因为感觉不经过严密思考,不太靠谱。很多人其实就是猜测,然后误打正着。本题严格论证起来非常复杂。

做题时,可以用dfs暴力找规律

所谓的删减,复制,就等价于每次添加一个前缀,复制一个串就是添加和上一个一样的前缀,复制若干个串的复合,就是添加若干个前缀罢了。而我们dfs就会囊括全部情况。

# include<iostream>
# include<algorithm>
# include<iomanip>

using namespace std;
typedef long long int ll;

int n,k;
string s;
string ans="zzzzzzzzzzzzzzz";
void dfs(string now,int len)
{

    if(len==k)
    {
        ans=min(ans,now);
        return  ;
    }
   for(int i=1;i+len<=k;i++)
   {
       string now1=now+s.substr(0,i);

       dfs(now1,now1.length());
   }
}
int main()
{

   cin>>n>>k;

   cin>>s;


   dfs("",0);

   cout<<ans;

    return 0;
}

之后会找到和网上一模一样的规律, 

# include<iostream>
# include<algorithm>
# include<iomanip>

using namespace std;
typedef long long int ll;


int main()
{
    int n,k;
    cin>>n>>k;

    string s;
    cin>>s;

    string ans="";

    for(int i=0;i<=k;i++)
    {
        ans+='z';
    }

  for(int len=1;len<=n;len++)
  {
      int cnt=k/len;

      int cnt2=k%len;

      string now="";
      string pre=s.substr(0,len);

      for(int i=1;i<=cnt;i++)
      {
          now+=pre;
      }
      now+=s.substr(0,cnt2);

      ans=min(ans,now);
  }

  cout<<ans;
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qinsanma and Code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值