C. Phone Numbers
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
And where the are the phone numbers?
You are given a string s consisting of lowercase English letters and an integer k. Find the lexicographically smallest string t of length k, such that its set of letters is a subset of the set of letters of s and s is lexicographically smaller than t.
It‘s guaranteed that the answer exists.
Note that the set of letters is a set, not a multiset. For example, the set of letters of abadaba is {a,?b,?d}.
String p is lexicographically smaller than string q, if p is a prefix of q, is not equal to q or there exists i, such that pi?<?qi and for all j?<?i it is satisfied that pj?=?qj. For example, abc is lexicographically smaller than abcd , abd is lexicographically smaller than abec, afa is not lexicographically smaller than ab and a is not lexicographically smaller than a.
Input
The first line of input contains two space separated integers n and k (1?≤?n,?k?≤?100?000) — the length of s and the required length of t.
The second line of input contains the string s consisting of n lowercase English letters.
Output
Output the string t conforming to the requirements above.
It‘s guaranteed that the answer exists.
Examples
inputCopy
3 3
abc
output
aca
inputCopy
3 2
abc
output
ac
inputCopy
3 3
ayy
output
yaa
inputCopy
2 3
ba
output
baa
Note
In the first example the list of strings t of length 3, such that the set of letters of t is a subset of letters of s is as follows: aaa, aab, aac, aba, abb, abc, aca, acb, .... Among them, those are lexicographically greater than abc: aca, acb, .... Out of those the lexicographically smallest is aca.
题目大意:给一个长度为n的字符串S,输出一个大于S的字典序的字符串中字典序最小的长度为k的字符串(考试的时候硬是没看懂T.T一直以为输出字典序最小的字符串)
分析:如果k<=n只用从后往前赋值,如果可以找到一个比该位字符字典序大,ans[i]=x,该位前面的直接等于ans[j]=s[j](j=i-1,j>=0,j--)即可,否则,ans[i]=min(s)(字符串s中最小的字母);
如果k>n,i<=n时,ans[i]=s[i];i>n,ans[i]=min(s);
#define debug
#include<stdio.h>
#include<math.h>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<functional>
#include<iomanip>
#include<map>
#include<set>
#define pb push_back
using namespace std;
typedef long long ll;
pair<ll,ll>PLL;
pair<int,ll>Pil;
const int INF = 0x3f3f3f3f;
const double inf=1e8+100;
const ll maxn =1e5+100;
const int N = 1e4+10;
const ll mod=1000007;
vector<int>v;
char s[maxn],ans[maxn];
int n,k;
bool flag;
void solve() {
int i,j,t=1;
//cin>>t;
while(t--) {
flag=0;
vector<int>::iterator it;
cin>>n>>k>>s;
// cout<<n<<" "<<s<<" "<<k<<endl;
for(i=0; i<n; i++) {
v.pb(s[i]-‘a‘);
}
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
for(i=k-1; i>=0; i--) {
if(k<=n) {
if(flag){
ans[i]=s[i];
continue;
}
int t=s[i]-‘a‘;
it=upper_bound(v.begin(),v.end(),t);
if(it==v.end()) {
ans[i]=v[0]+‘a‘;
// cout<<ans[i]<<" k<=n ";
} else {
ans[i]=*it+‘a‘;
// cout<<ans[i]<<" k<=n ";
flag=1;
}
} else {
if(flag) {
ans[i]=s[i];
// cout<<ans[i]<<" k>n ";
} else {
for(;i>=n;i--){
ans[i]=v[0]+‘a‘;
// cout<<ans[i]<<" k>n ";
}
flag=1;
i++;//这里不要写落了
}
}
}
// cout<<endl;
cout<<ans<<endl;
// v.clear();
// memset(ans,‘\0‘,sizeof(ans));
}
}
int main() {
ios_base::sync_with_stdio(false);
#ifdef debug
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
cin.tie(0);
cout.tie(0);
solve();
return 0;
}