http://codeforces.com/problemset/problem/940/C

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 aband 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
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 sis 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.

思维题

1.s.size()>len 直接往后加最小的字符

2.其他情况,从后往前找(不是最大字符)的第一个字符,将它改为字典序下一个字符,然后将它后面的字符全改为最小字符

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf 2147483647
const ll INF = 0x3f3f3f3f3f3f3f3fll;
#define ri register int
template <class T> inline T min(T a, T b, T c) { return min(min(a, b), c); }
template <class T> inline T max(T a, T b, T c) { return max(max(a, b), c); }
template <class T> inline T min(T a, T b, T c, T d) {
return min(min(a, b), min(c, d));
}
template <class T> inline T max(T a, T b, T c, T d) {
return max(max(a, b), max(c, d));
}
#define scanf1(x) scanf("%d", &x)
#define scanf2(x, y) scanf("%d%d", &x, &y)
#define scanf3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define scanf4(x, y, z, X) scanf("%d%d%d%d", &x, &y, &z, &X)
#define pi acos(-1)
#define me(x, y) memset(x, y, sizeof(x));
#define For(i, a, b) for (int i = a; i <= b; i++)
#define FFor(i, a, b) for (int i = a; i >= b; i--)
#define bug printf("***********\n");
#define mp make_pair
#define pb push_back
const int maxn = ;
// name*******************************
string s, s1;
int len;
vector<char> vec;
bool vis[maxn];
int n;
// function****************************** //***************************************
int main() {
// ios::sync_with_stdio(0);
// cin.tie(0);
// freopen("test.txt", "r", stdin);
// freopen("outout.txt","w",stdout);
cin >> n >> len;
cin>>s;
For(i, , s.size() - ) {
if (vis[s[i]] == ) {
vec.pb(s[i]);
}
}
sort(vec.begin(), vec.end());
s1 = s;
// cout<<len<<" "<<s1.size()<<endl;
if (len > s1.size()) {
cout<<s1;
For(i, , len - s1.size())cout<<vec.front();
return ;
}
s1 = s.substr(, len);
FFor(i, len - , ) {
char c = s1[i];
if (c == vec.back())
continue;
FFor(j, vec.size() - , ) {
if (c == vec[j]) {
s1[i] = vec[j + ];
For(k, i + , len - ) { s1[k] = vec.front(); }
cout << s1;
return ;
}
}
} return ;
}
 

最新文章

  1. linux mysql-5.6.26 安装
  2. Effective C++ -----条款48:认识template元编程
  3. java链式编程设计
  4. JavaScript基础--DOM对象(十三):(windows对象:history\location\navigator\screen\event)
  5. Android实现圆形圆角图片
  6. hibernate有关联关系删除子表时可能会报错,可以用个clear避免错误
  7. HDU 4629 Burning 几何 + 扫描线
  8. Linux系统搭建负载均衡环境
  9. C#的装箱和拆箱
  10. 调试exynos4412—ARM嵌入式Linux—LEDS/GPIO驱动之一
  11. 使用C#和.NET 4编写的并行应用程序“多核并发编程的规则”
  12. map map
  13. 利用workbench将excel数据导入到MySQL中
  14. [转载] 解读ClassLoader
  15. 前端架构师 摘自《前端架构设计》-micah godbolt
  16. memset的用法
  17. (92)Wangdao.com_第二十五天_线程机制_H5 Web Workers 分线程任务_事件 Event
  18. webpack的安装及使用
  19. 数字对讲系统开发札记(前端linux c 后端 c#)
  20. MVC 下拉列表三级联动

热门文章

  1. 微信获取openId
  2. drupal7 addExpression+union+分页
  3. FastDFS部署安装全过程
  4. js map()处理数组和对象数据
  5. zookeeper - java操作
  6. node和iisnode express手工安装
  7. 的确,Java存在缺陷。但是……
  8. LeetCode题解之Hamming Distance
  9. RAC性能分析 - gc buffer busy acquire 等待事件
  10. 优化REST Framework 的 路由 APIView 和ViewSetMixin