Phone Numbers

 

C. Phone Numbers

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 notlexicographically 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 oft.

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

input

Copy

3 3
abc

output

aca

input

Copy

3 2
abc

output

ac

input

Copy

3 3
ayy

output

yaa

input

Copy

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.

有道翻译:

你给出一个字符串s由小写英文字母和一个整数k。找到按最小的字符串长度t的k,这样它的字母的一个子集组字母s和s按小于t。

保证答案的存在。注意字母的集合是一组,不是一个多重集。例如,一组字母abadaba是{ a,b,d }。p是按小于字符串的字符串,如果p是一个前缀,不等于q或存在,这样pi < qi,对于所有j < i都满足pj = qj。例如,abc字母角度小于abcd abd比abec小字母顺序,afa在字典上比ab小,而a在字典上比a小。

 

输入输入的第一行包含两个整数n和k(1≤n,k≤100)- s和t的所需长度的长度。第二行输入包含字符串的s n小写英文字母组成。输出输出字符串t符合上面的要求。保证答案的存在。

 

就是说,只能用s的集合里的字符,组成长度为k的字符串t。t要大于s且要求t最小

 

我再一次想到贪心,分两种情况。

1.k>n那么只要前面和s一样后面k-n个都是集合中最小的字符

2.k<=n。因为字典序中字符串大小先是由下标小的决定的。所以尽量保证前面不变。后面往前找,第一个找到能被替换掉的字符(即集合中有字符大于这个字符)时,前面不变这个变成大于这个字符且最小的字符,后面全是集合中最小的字符。

#include<bits/stdc++.h>
using namespace std;
bool w[30];
int main()
{
	int n,k,i,j,l,r;
	string s;
	cin>>n>>k;
	cin>>s;
	for(i=0;i<s.size();i++)
		w[s[i]-'a'+1]=1;	    //保存字符 
	for(i=1;i<=26;i++)		    //找到集合中最小字符,l为其ASCII码 
		if(w[i]==1){
			l=i+'a'-1;
			break;
		}
	for(i=26;i>=1;i--)		    //找到集合中最大字符,r为其ASCII码 
		if(w[i]==1){
			r=i+'a'-1;
			break;
		}
	if(k>n){			    //第一种情况 
		cout<<s;   
		for(i=1;i<=k-n;i++)
			cout<<char(l);
		return 0;
	}
	for(i=k-1;i>=0;i--){
		if(r>s[i]){		    //如果有r大于s[i],那么集合中肯定有字符大于s[i] 
			cout<<s.substr(0,i);//输出前面 
			for(j=s[i]-'a'+2;j<=26;j++){
				if(w[j]){    //找到第一个大于s[i],的字符输出 
					cout<<char(j+'a'-1);
					break;
				}
			}
			for(j=i+1;j<k;j++)    //输出后面 
				cout<<char(l);
			return 0;
		}
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值