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的更多相关文章

  1. 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 ...

  2. Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F

    Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...

  3. Codeforces Round #466 (Div. 2) E. Cashback

    Codeforces Round #466 (Div. 2) E. Cashback(dp + 贪心) 题意: 给一个长度为\(n\)的序列\(a_i\),给出一个整数\(c\) 定义序列中一段长度为 ...

  4. 构造 Codeforces Round #107 (Div. 2) B. Phone Numbers

    题目传送门 /* 构造:结构体排个序,写的有些啰嗦,主要想用用流,少些了判断条件WA好几次:( */ #include <cstdio> #include <algorithm> ...

  5. Codeforces Round #466 (Div. 2) Solution

    从这里开始 题目列表 小结 Problem A Points on the line Problem B Our Tanya is Crying Out Loud Problem C Phone Nu ...

  6. Codeforces Round #181 (Div. 2) C. Beautiful Numbers 排列组合 暴力

    C. Beautiful Numbers 题目连接: http://www.codeforces.com/contest/300/problem/C Description Vitaly is a v ...

  7. Codeforces Round #466 (Div. 2)

    所有的题目都可以在CodeForces上查看 中间看起来有很多场比赛我没有写了 其实是因为有题目没改完 因为我不想改,所以就没有写了(大部分题目还是改完了的) 我还是觉得如果是打了的比赛就一场一场写比 ...

  8. Codeforces Round #466 (Div. 2) 题解

    人生中第三次\(CF\)... 考试中切了\(A\)~\(E\) \(F\)题会做没时间写 题解 A:Points on the line 题意 给定一个数列,删最小的数,使最大差不大于一个定值 So ...

  9. 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 ...

随机推荐

  1. Error:dojo.data.ItemFileWriteStore:Invalid item argument

    1.错误描述 dijit.form.ComboBox TypeError:_4e is undefined                                            Sea ...

  2. Java中的List转换成JSON报错(一)

    1.错误描述 Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/ ...

  3. 利用ICSharpCode.SharpZipLib进行压缩

    #ZipLib is a Zip, GZip, Tar and BZip2 library written entirely in C# for the .NET platform. It is im ...

  4. Django学习-6-路由系统

    1.url(r'^index/', views.index), 函数处理       url(r'^home/', views.Home.as_view()),           类方法处理     ...

  5. iOS - MFi 认证

    1.MFi 认证 1.1 什么是 MFi 认证 苹果 MFi 认证,是苹果公司(Apple Inc.)对其授权配件厂商生产的外置配件的一种标识使用许可,是 Apple 公司 "Made fo ...

  6. Java百度地图经纬度纠偏

    在国内使用电子地图获取到的经纬度都不是真实的经纬度,而是经过一定的算法在真实的经纬度上添加了一个偏移量,且不同的地图有不同的算法.现在告诉大家在java中怎样对百度地图进行纠偏,主要实现将真实的经纬度 ...

  7. Linux进行AES加密每次结果都不一致并且解密失败报错

    1. 现象 windows操作系统下进行"123456"的AES加密 encrypted message is below : QLNYZyjRnKF/zxAjzDt/lw== d ...

  8. POJ 2516 Minimum Cost (费用流)

    题面 Dearboy, a goods victualer, now comes to a big problem, and he needs your help. In his sale area ...

  9. luogu 访问”美术馆“ && 偷天换日

    把这两个题放在一起,是因为这两个题差不多,难度递进其实是懒得写两个博客所以一起分享一下 访问“美术馆” 题目描述 经过数月的精心准备,Peer Brelstet,一个出了名的盗画者,准备开始他的下一个 ...

  10. Bzoj1901 Dynamic Ranking

    动态区间第k小 离散化后 那么每个点开一棵线段树(主席树)再套一个树状数组在外面 每次询问区间内的树的个数时 相当于进行了一次树状数组求区间和的操作,只是是把树状数组那个点看做主席树,对log棵主席树 ...