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 ...
随机推荐
- DML触发器1
数据库触发器是特殊的存储程序.通常不直接调用它们,而是由数据库的事件触发. 触发器分类: >DML触发器 >instead-of触发器 >系统事件触发器,还可以是DDL 一. DML ...
- 利用Tomcat部署Web项目报错
1.错误描述 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help ...
- Struts+Spring+Hibernate、MVC、HTML、JSP
javaWeb应用 JavaWeb使用的技术,比如SSH(Struts.Spring.Hibernate).MVC.HTML.JSP等等技术,利用这些技术开发的Web应用在政府项目中非常受欢迎. 先说 ...
- iOS - XMPP Openfire 服务器的搭建
前言 提前下载好相关软件,且安装目录最好安装在全英文路径下.如果路径有中文名,那么可能会出现一些莫名其妙的问题. 提前准备好的软件: jdk-8u91-macosx-x64.dmg mysql-5.7 ...
- poi 导入导出的api说明(大全)
原文链接:http://www.cnblogs.com/qingruihappy/p/8443101.html poi 导入导出的api说明(大全) 一. POI简介 ApachePOI是Apache ...
- 手机浏览网页或打开App时莫名弹出支付宝领红包界面的原因及应对措施
自从支付宝推出扫码领红包活动后,这种模式独特的赏金机制,短时间内吸引了大量的关注,但是随之也产生了很多的问题,比由于如在赏金的驱动下,微信群里铺天盖地的红包口令,朋友圈里各式各样的领红包二维码图片, ...
- View的放大->旋转->还原动画
以UIButton为例,创建一个类,继承于UIButton /*页面的创建用storyboard*/ .h文件 @interface PTSRecommendButton : UIButton - ...
- I2C总线协议的软件模拟实现方法
I2C总线协议的软件模拟实现方法 在上一篇博客中已经讲过I2C总线通信协议,本文讲述I2C总线协议的软件模拟实现方法. 1. 简述 所谓的I2C总线协议的软件模拟实现方法,就是用软件控制GPIO的输入 ...
- 从FCN到DeepLab
图像语义分割,简单而言就是给定一张图片,对图片上的每一个像素点分类. 图像语义分割,从FCN把深度学习引入这个任务,一个通用的框架事:前端使用FCN全卷积网络输出粗糙的label map,后端使用CR ...
- Python魔法方法(转发整合)
如果你的对象实现(重载)了这些方法中的某一个,那么这个方法就会在特殊的情况下被 Python 所调用,你可以定义自己想要的行为,而这一切都是自动发生的. __new__: 是一个对象实例化时调用的第一 ...