问题连接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;
}