-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path940C. Phone Numbers.cpp
69 lines (53 loc) · 1.06 KB
/
940C. Phone Numbers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <bits/stdc++.h>
using namespace std;
int n, k;
bool st[26];
string s;
int main() {
cin >> n >> k >> s;
for(int i = 0; i < n; ++i)
st[s[i] - 'a'] = true;
char sm;
for(int i = 0; i < 26; ++i)
if(st[i]) {
sm = i + 'a';
break;
}
if(n < k) {
cout << s;
for(int i = 0; i < k - n; ++i)
cout << sm;
cout << endl;
return 0;
}
if(n > k) {
int idx;
char ch = ' ';
for(int i = k - 1; ch == ' ' && i >= 0; --i)
for(int j = 0; j < 26; ++j)
if(st[j] && j > (s[i] - 'a')) {
idx = i;
ch = j + 'a';
break;
}
s[idx] = ch;
for(int i = idx + 1; i < k; ++i)
s[i] = sm;
cout << s.substr(0, k) << endl;
return 0;
}
int idx;
char ch = ' ';
for(int i = n - 1; ch == ' ' && i >= 0; --i)
for(int j = 0; j < 26; ++j)
if(st[j] && j > (s[i] - 'a')) {
idx = i;
ch = j + 'a';
break;
}
s[idx] = ch;
for(int i = idx + 1; i < n; ++i)
s[i] = sm;
cout << s << endl;
return 0;
}