940C Phone Numbers
题目大意
给你两个数字n和k,给你一个字符串s,n是s的长度,求字母集合是s的字母集合子集的字典序大于s的长度为k的字典序最小的字符串t
分析
将字符转化为数字,然后分两种情况处理:
1.n<k:t的前n为是s,后面几位是s中字典序最小的字母
2.n>=k:将t赋为s的前k位,从t的最后一位开始,如果字符子集中有字典序比它大的则将其替换为比其大的中字典序最小的字符,否则将其替换为整个字符子集中字典序最小的字符,在此位的前一位重复此步骤
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
char s[110000],t[110000];
bool is[50];
int a[110000],b[110000],c[50];
int n,m,j,k,cnt=0;
inline void gob(int pl){
if(b[pl-1]<c[cnt]){
for(int i=1;i<=cnt;i++)
if(c[i]>b[pl-1]){
b[pl-1]=c[i];
return;
}
}
else {
b[pl-1]=c[1];
gob(pl-1);
}
}
int main()
{ int i;
cin>>n>>k;
scanf("%s",s+1);
for(i=1;i<=n;i++){
a[i]=s[i]-'a';
if(!is[a[i]])c[++cnt]=a[i];
is[a[i]]=1;
}
sort(c+1,c+1+cnt);
for(i=1;i<=min(n,k);i++){
b[i]=a[i];
}
if(k>n){
for(i=n+1;i<=k;i++)b[i]=c[1];
}
else {
if(is[b[k]+1]){
b[k]+=1;
}
else if(b[k]<c[cnt]){
for(i=1;i<=cnt;i++)
if(c[i]>b[k]){
b[k]=c[i];
break;
}
}
else {
b[k]=c[1];
gob(k);
}
}
for(i=1;i<=k;i++)
t[i]=(b[i]+'a');
printf("%s",t+1);
return 0;
}
940C Phone Numbers的更多相关文章
- 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 - 940C + CodeForces - 932B (两道比较好的模拟题)
940C链接:http://codeforces.com/problemset/problem/940/C C. Phone Numbers time limit per test 2 seconds ...
- 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 ...
随机推荐
- xshell 与 putty
http://blog.csdn.net/efine_dxq/article/details/54599184 一.Xshell 与 putty概念 Xshell是一个强大的安全终端模拟软件,它支持S ...
- PostgreSQL9.6.2的WINDOWS下安装
下载链接:https://www.enterprisedb.com/downloads/postgres-postgresql-downloads#windows 安装链接:http://www.cn ...
- Nodejs+Grunt配置SASS项目自动编译
Nodejs+Grunt配置SASS项目自动编译 早前听说Nodejs和Grunt很强大,特别是用来构建自动化的前端开发,更是强大无比.但一直碍于自己掌握的技术有限,不敢深入,也未曾深入下去.最近在开 ...
- sql 时间转换问题 from_unixtime() UNIX_TIMESTAMP()
http://blog.csdn.net/test_soy/article/details/50328367 from_unixtime()是MySQL里的时间函数 date为需要处理的参数(该参数是 ...
- python3 第十二章 - 数据类型之List(列表)
Python内置的一种数据类型是列表:list. list是一种有序的集合 可以随时添加和删除其中的元素. 它可以作为一个方括号内的逗号分隔值出现. 列表的数据项不需要具有相同的类型 创建一个列表,只 ...
- impala集成sentry
1.安装配置sentry 详细步骤见上一篇安装配置sentry. 2.配置impala 注:以下配置未集成kerberos安全认证 在/etc/imapla/conf目录下创建sentry-site. ...
- JS 中的事件设计
看懂此文,不再困惑于 JS 中的事件设计 原文出处: aitangyong 抽空学习了下javascript和jquery的事件设计,收获颇大,总结此贴,和大家分享. (一)事件绑定的几种方式 ...
- The server's host key is not cached in the registry. You have no guarantee that the server……
使用putty中的pscp.exe ,可以通过脚本方式实现windows向linux上传文件,但pscp.exe第一次运行时必须手工输入确认信息,本文主要解决掉初次运行时的人工交互,彻底实现静默运行. ...
- linux下安装pip
1.pip下载安装 wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d ...
- Sql Server的艺术(二) SQL复杂条件搜索
本次讲到where字句中经常用到的集中较为复杂的搜索条件,包括组合的查询条件.IN运算符.NOT运算符.LIKE运算符和相关通配符. 学习本节需要用到一下两张表: CREATE TABLE TEACH ...