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 ;
}
C. Phone Numbers的更多相关文章
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Bitwise AND of Numbers Range 数字范围位相与
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...
- [LeetCode] Valid Phone Numbers 验证电话号码
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...
- [LeetCode] Consecutive Numbers 连续的数字
Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...
- [LeetCode] Compare Version Numbers 版本比较
Compare two version numbers version1 and version1.If version1 > version2 return 1, if version1 &l ...
- [LeetCode] Sum Root to Leaf Numbers 求根到叶节点数字之和
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number ...
随机推荐
- [WEB面试题] web前端面试题JavaScript第一弹,个人整理部分面试题汇总
以下内容仅供参考,成年人不讲对错只讲利弊 1.什么是JavaScript原型链?如何理解 JavaScript中的每个对象都有一个prototype属性,我们称之为原型 原型的值是一个对象有自己的原型 ...
- js实现放大镜的效果
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Python 底层原理知识
1.Python是如何进行内存管理的? 答:从三个方面来说,一对象的引用计数机制,二垃圾回收机制,三内存池机制 一.对象的引用计数机制 Python内部使用引用计数,来保持追踪内存中的对象,所有对象都 ...
- 报表导出excel方式介绍
报表导出excel提供了四种方式,在单元格属性"其他/导出excel方式"可以选择,如下图 一是导出缺省值:报表中的单元格包含两个值,一个真实值一个显示值,但是在excel中 ...
- BlockingQueue介绍及使用
1.BlockingQueue队列和平常队列一样都可以用来作为存储数据的容器,但有时候在线程当中 涉及到数据存储的时候就会出现问题,而BlockingQueue是空的话,如果一个线程要从Blockin ...
- Java 8方法引用使用指南
[编者按]本文作者为拥有15年 Java 开发经验的资深程序员 Per-Åke Minborg,主要介绍如何灵活地解析 Java 中的方法引用.文章系国内 ITOM 管理平台 OneAPM 编译呈现. ...
- AWK与SED命令
linux系统比较常用的AWK与SED命令,这两个命令主要是格式化文本文件信息.接下来将详细介绍这两个命令的基本用法以及可以实现的功能. 一.AWK命令 AWK语言的基本功能是在文件或者字符串中基于指 ...
- 【MySQL】无法启动mysql服务(位于本地计算机上)错误1067,进程意外中止
好久没看MySQL了,今天启动起来找找感觉,尴尬了...发现服务启动不了.系统提示:无法启动mysql服务(位于本地计算机上)错误1067,进程意外中止. 解决过程: 1.在网上百度好久,看到一条解决 ...
- redis集群热扩展(基于4.0.9)
1:环境说明,首先说一下要做的事情,我们要迁移redis集群槽位,现有redis集群环境如下 我们看一下集群的基本信息: > cluster nodes 8ea64a0049e0b193296a ...
- npm run dev时报错“events.js:160 throw er; // Unhandled 'error' event”
经查,此问题由端口占用导致,node服务器默认端口8080已被其他程序占用,关闭占用端口的程序或者修改node服务器的默认端口即可解决此问题