codeforce round#466(div.2)C. Phone Numbers
C. Phone Numbers
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 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 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
inputCopy
3 3
abc
output
aca
inputCopy
3 2
abc
output
ac
inputCopy
3 3
ayy
output
yaa
inputCopy
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.
题目大意:给一个长度为n的字符串S,输出一个大于S的字典序的字符串中字典序最小的长度为k的字符串(考试的时候硬是没看懂T.T一直以为输出字典序最小的字符串)
分析:如果k<=n只用从后往前赋值,如果可以找到一个比该位字符字典序大,ans[i]=x,该位前面的直接等于ans[j]=s[j](j=i-1,j>=0,j--)即可,否则,ans[i]=min(s)(字符串s中最小的字母);
如果k>n,i<=n时,ans[i]=s[i];i>n,ans[i]=min(s);
#define debug
#include<stdio.h>
#include<math.h>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<functional>
#include<iomanip>
#include<map>
#include<set>
#define pb push_back
using namespace std;
typedef long long ll;
pair<ll,ll>PLL;
pair<int,ll>Pil;
const int INF = 0x3f3f3f3f;
const double inf=1e8+100;
const ll maxn =1e5+100;
const int N = 1e4+10;
const ll mod=1000007;
vector<int>v;
char s[maxn],ans[maxn];
int n,k;
bool flag;
void solve() {
int i,j,t=1;
//cin>>t;
while(t--) {
flag=0;
vector<int>::iterator it;
cin>>n>>k>>s;
// cout<<n<<" "<<s<<" "<<k<<endl;
for(i=0; i<n; i++) {
v.pb(s[i]-'a');
}
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
for(i=k-1; i>=0; i--) {
if(k<=n) {
if(flag){
ans[i]=s[i];
continue;
}
int t=s[i]-'a';
it=upper_bound(v.begin(),v.end(),t);
if(it==v.end()) {
ans[i]=v[0]+'a';
// cout<<ans[i]<<" k<=n ";
} else {
ans[i]=*it+'a';
// cout<<ans[i]<<" k<=n ";
flag=1;
}
} else {
if(flag) {
ans[i]=s[i];
// cout<<ans[i]<<" k>n ";
} else {
for(;i>=n;i--){
ans[i]=v[0]+'a';
// cout<<ans[i]<<" k>n ";
}
flag=1;
i++;//这里不要写落了
}
}
}
// cout<<endl;
cout<<ans<<endl;
// v.clear();
// memset(ans,'\0',sizeof(ans));
}
} int main() {
ios_base::sync_with_stdio(false);
#ifdef debug
freopen("in.txt", "r", stdin);
// freopen("out.txt","w",stdout);
#endif
cin.tie(0);
cout.tie(0);
solve();
return 0;
}
codeforce round#466(div.2)C. Phone Numbers的更多相关文章
- codeforce round#466(div.2) B. Our Tanya is Crying Out Loud
B. Our Tanya is Crying Out Loud time limit per test1 second memory limit per test256 megabytes input ...
- Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F
Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...
- Codeforces Round #466 (Div. 2) E. Cashback
Codeforces Round #466 (Div. 2) E. Cashback(dp + 贪心) 题意: 给一个长度为\(n\)的序列\(a_i\),给出一个整数\(c\) 定义序列中一段长度为 ...
- 构造 Codeforces Round #107 (Div. 2) B. Phone Numbers
题目传送门 /* 构造:结构体排个序,写的有些啰嗦,主要想用用流,少些了判断条件WA好几次:( */ #include <cstdio> #include <algorithm> ...
- Codeforces Round #466 (Div. 2) Solution
从这里开始 题目列表 小结 Problem A Points on the line Problem B Our Tanya is Crying Out Loud Problem C Phone Nu ...
- Codeforces Round #181 (Div. 2) C. Beautiful Numbers 排列组合 暴力
C. Beautiful Numbers 题目连接: http://www.codeforces.com/contest/300/problem/C Description Vitaly is a v ...
- Codeforces Round #466 (Div. 2)
所有的题目都可以在CodeForces上查看 中间看起来有很多场比赛我没有写了 其实是因为有题目没改完 因为我不想改,所以就没有写了(大部分题目还是改完了的) 我还是觉得如果是打了的比赛就一场一场写比 ...
- Codeforces Round #466 (Div. 2) 题解
人生中第三次\(CF\)... 考试中切了\(A\)~\(E\) \(F\)题会做没时间写 题解 A:Points on the line 题意 给定一个数列,删最小的数,使最大差不大于一个定值 So ...
- Codeforces Round #466 (Div. 2) -A. Points on the line
2018-02-25 http://codeforces.com/contest/940/problem/A A. Points on the line time limit per test 1 s ...
随机推荐
- Postgresql基本用法以及优化注意
本篇为之前学习PG数据库一些基本操作使用以及优化注意. 比较运算符 = : <>,!=,<= , <,>=,>: 不能用于null判断; Between: Le ...
- 在laravel环境下将图片存入MongoDB数据库
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,bi ...
- 一些比较隐秘的OJ的网址
( 1 )COGS:cogs.pro:8080 ( 2 )AGC:agc012.contest.atcoder.jp ( 3 )CS Acaemy:csacademy.com ( 4 )JoyOI:w ...
- datatable 参数详细说明
页面参数说明 var docrTable = $('#docrevisontable').dataTable({ language:lang, //提示信息 autoWidth: false, //禁 ...
- Myeclipse插件快速生成ssh项目并配置注解 在action层注入service的超详细过程
最近发现,我对于ssh的 自动注入配置 还是不熟悉,于是整理了一下 终于做了一个 简单的 注入配置出来. 以前都是在applicationContext.xml 里面这样配 <bean id=& ...
- PHP Curl会话请求
/** * @param string $url 请求地址 * @param string $type 请求类型 post get * @param string $arr 如果是post 传递的数据 ...
- 大数据(3):基于sogou.500w.utf8数据Hbase和Spark实践
1. HBase安装部署操作 a) 解压HBase安装包tar –zxvf hbase-0.98.0-hadoop2-bin.tar.gzb) 修改环境变量 hbase-env.shexport JA ...
- VLOOKUP和MATCH嵌套以高效引用多列数据
VLOOKUP函数在日常工作中十分常见,以至于你要是没用过VLOOKUP函数,你都不好意思说你懂EXCEL. 一般情况下,我们需要在源数据中查找某个指定列的数据,就会用到VLOOKUP函数(如果是指定 ...
- IPFS: NAT traversal(NAT穿越)
IPFS是一个p2p网络,那么一定绕不开的一个问题就是NAT穿越.之前的文章里面也提到过IPFS网络连通性使用的ICE NAT穿越框架,本文简单介绍一下什么是NAT. 为什么有NAT技术? NAT ...
- 读取pdf内容分页和全部
//读取pdf 全部内容public static String topdffile(String pdffile){ StringBuffer result = new StringBuffer() ...