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 ...
随机推荐
- IO 异常:The Network Adapter could not establish the connection 怎么解决
IO 异常:The Network Adapter could not establish the connection 怎么解决
- JavaScript学习日志:关于js分号
javascript有自动添加分号的功能,但是不是所有情况都会自动添加,要区分: 1,如果语句独占一行 如果当前行内的语句能够被js正确解析,那么就会在句尾添加一个分号. (如何判断是否正确解析?你在 ...
- 爬虫_url去重策略
如何对url去重? 将访问url保存到数据库中,效率低,最简单 将url保存到set中,查询速度快,但当url达到1亿多条时候,占用太多内存空间 将url经过md5等方法哈希后保存到set中 用bit ...
- Linux指令--性能监控和优化命令相关指令
原文出处:http://www.cnblogs.com/peida/archive/2012/12/05/2803591.html.感谢作者无私分享 性能监控和优化命令相关指令有:top,free,v ...
- Go基础--goroutine和channel
goroutine 在go语言中,每一个并发的执行单元叫做一个goroutine 这里说到并发,所以先解释一下并发和并行的概念: 并发:逻辑上具备同时处理多个任务的能力 并行:物理上在同一时刻执行多个 ...
- 00_HTML入门第一天
HTML入门 body标记的常见属性:bgcolor 设置背景颜色:text 设置文本颜色:link 设置链接颜色:vlink 设置已经访问了的链接颜色:alink 正在点击的链接颜色: meta是单 ...
- c#结构体、打他table、excel、csv互转
1.csv相关 public static class CsvHelper { /// <summary> /// 根据csv路径获取datatable /// </summary& ...
- radare2逆向笔记
最近刚开始学习逆向(Reverse Engineering), 发现其学习曲线也是挺陡峭的, 而网上的 许多writeup文章主旨总结就六个字:"你们看我屌吗?" ...几近炫技而 ...
- MySQL用户授权与权限
MySQL权限如下表 权限名字 权限说明 Context CREATE 允许创建新的数据库和表 Databases, tables, or indexes DROP 允许删除现有数据库.表和视图 Da ...
- dubbo 线程池
在dubbo调用过程中被调用方有两个线程池:io线程池,业务线程池. 这也是dubbo调优的点. 配置信息: <dubbo:protocol name="dubbo" dis ...