C. Phone Numbers
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.
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 the string t conforming to the requirements above.
It's guaranteed that the answer exists.
3 3
abc
aca
3 2
abc
ac
3 3
ayy
yaa
2 3
ba
baa
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 ;
}
最新文章
- linux mysql-5.6.26 安装
- Effective C++ -----条款48:认识template元编程
- java链式编程设计
- JavaScript基础--DOM对象(十三):(windows对象:history\location\navigator\screen\event)
- Android实现圆形圆角图片
- hibernate有关联关系删除子表时可能会报错,可以用个clear避免错误
- HDU 4629 Burning 几何 + 扫描线
- Linux系统搭建负载均衡环境
- C#的装箱和拆箱
- 调试exynos4412—ARM嵌入式Linux—LEDS/GPIO驱动之一
- 使用C#和.NET 4编写的并行应用程序“多核并发编程的规则”
- map map
- 利用workbench将excel数据导入到MySQL中
- [转载] 解读ClassLoader
- 前端架构师 摘自《前端架构设计》-micah godbolt
- memset的用法
- (92)Wangdao.com_第二十五天_线程机制_H5 Web Workers 分线程任务_事件 Event
- webpack的安装及使用
- 数字对讲系统开发札记(前端linux c 后端 c#)
- MVC 下拉列表三级联动