And where the are the phone numbers?
You are given a string s consisting of lowercase English letters and an integerk. Find the lexicographically smallest stringt of length k, such that its set of letters is a subset of the set of letters ofs and s is lexicographically smaller thant.
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 ofabadaba is {a, b, d}.
String p is lexicographically smaller than stringq, if p is a prefix ofq, is not equal to q or there exists i, such thatpi < qi and for allj < i it is satisfied that pj = qj. For example,abc is lexicographically smaller than abcd , abd is lexicographically smaller thanabec, afais 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 ofs and the required length of t.
The second line of input contains the string s consisting ofn lowercase English letters.
Output
Output the string t conforming to the requirements above.
It’s guaranteed that the answer exists.
Example
Input
3 3
abc
Output
aca
Input
3 2
abc
Output
ac
Input
3 3
ayy
Output
yaa
Input
2 3
ba
Output
baa
Note
In the first example the list of strings t of length 3, such that the set of letters oft is a subset of letters of s is as follows: aaa, aab, aac, aba, abb, abc, aca, acb, …. Among them, those are lexicographically greater thanabc: aca,acb, …. Out of those the lexicographically smallest isaca.
题意分析:
这道题就是写出比给出字符串字典序大的最小字符串,要求所用字符串的字符来自与给出的原来字符串。
比如说给出
3 3(原字符串的长度,输出字符串的长度)
ayy
输出
yaa
最近刚刚学STL,写的不好。
通过写这道题目,知道了set不能访问指定位置的字符串,只能遍历访问。 知道了vecotr的尾部元素的地址是ite=ve.end()-1
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define For(a,b) for(int a=0;a<b;a++)
#define mem(x) memset(x,0,sizeof(x))
#define Debug(x) cout<<"---> "<<x<<endl;
#define sf scanf
#define pf printf
int gcd(int a,int b){ return b>0?gcd(b,a%b):a;}
typedef long long ll;
typedef pair<int ,int > P;
//head
#define maxn 300100
int n,k;
char str[maxn];
int A[maxn];
set <int> ss;
set<int>::iterator ite;
vector<int> ve;
vector<int>::iterator it;
int B[maxn],len;
char a;
int main(){
int pos;
cin>>n>>k;
For(i,n) {
cin>>str[i];
A[i]=str[i]-'0'-49;
ss.insert(A[i]);
}
for(ite=ss.begin();ite!=ss.end();++ite){
ve.push_back(*ite);
B[len++]=*ite;
}
it=ve.end()-1; //访问尾部元素的注意点
for(int i=k-1;i>=0;i--){
if(A[i]==*it){
A[i]=*ve.begin();
}
else{
int pos=upper_bound(B,B+len,A[i])-B;
A[i]=B[pos];
break;
}
}
//print
if(k<=n){
for(int i=0;i<k;i++){
a=A[i]+49+'0';
cout<<a;
}
}
else{
for(int i=0;i<n;i++){
a=A[i]+49+'0';
cout<<a;
}
for(int i=0;i<k-n;i++){
a=B[0]+49+'0';
cout<<a;
}
}
cout<<endl;
return 0;
}