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,写的不好。
#include
using namespace std;
#define inf 0x3f3f3f3f
#define For(a,b) for(int a=0;a
#define mem(x) memset(x,0,sizeof(x))
#define Debug(x) cout<<"---> "<
#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;i49 +'0';
cout<else{
for(int i=0;i49+'0';
cout<for(int i=0;i0]+49+'0';
cout<cout<return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define inf 0x3f3f3f
#define sf scanf
#define pf printf
using namespace std;
typedef long long ll;
typedef pair<int ,int > P;
ll gcd(ll a,ll b) { return b>0?gcd(b,a%b):a;}
ll lcm(ll a,ll b){ return a*b/gcd(a,b);}
//head
#define maxn 20000
using namespace std;
typedef long long ll;
char str;
set<int> s;
int ha[maxn];
int A[maxn],B[maxn];
int n,k;
int main(){
cin>>n>>k;
for(int i=0;icin>>str;
A[i]=str-'a';
s.insert(A[i]);
}
int p=0;
set<int>::iterator ite;
for(ite=s.begin();ite!=s.end();++ite){
B[p++]=*ite;
}
p=s.size()-1;
for(int i=n-1;i>=0;i--){
if(A[i]==B[p]){
A[i]=B[0];
}
else{
int pos=upper_bound(B,B+p,A[i])-B;
A[i]=B[pos];
break;
}
}
//print
if(k<=n){
for(int i=0;i'a';
cout<else{
for(int i=0;i'a';
cout<for(int i=0;i0]+'a';
cout<cout<return 0;
}