传送门

题目大意

给你两个数字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的更多相关文章

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

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

  2. CodeForces - 940C + CodeForces - 932B (两道比较好的模拟题)

    940C链接:http://codeforces.com/problemset/problem/940/C C. Phone Numbers time limit per test 2 seconds ...

  3. Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range

    在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...

  4. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  5. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  6. [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 ...

  7. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  8. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  9. [LeetCode] Valid Phone Numbers 验证电话号码

    Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bas ...

随机推荐

  1. 查询A、B表中,A表中B表没有的数据

    A.B两表,找出ID字段中,存在A表,但是不存在B表的数据.A表总共13w数据,去重后大约3W条数据,B表有2W条数据,且B表的ID字段有索引. 方法一 使用 not in ,容易理解,效率低  ~执 ...

  2. 《并行程序设计导论》——MPI(Microsoft MPI)(6):并行排序算法

    =================================版权声明================================= 版权声明:原创文章 禁止转载  请通过右侧公告中的“联系邮 ...

  3. js截取字符串的方法

    1,slice(a, b) 第一个参数表示起始位置,第二个表示截取到但不包含 关于参数正负问题,只要记住一点:永远不能倒着截取!否则返回空字符串 2,substring(a, b) 第一个参数表示起始 ...

  4. jQuery hover() 方法

    $("p").hover(function(){ $("p").css("background-color","yellow&qu ...

  5. Java代码编写的一般性指导

    (1) 命名规则:这个最基本,也最重要,请牢记. 1,类名首字母应该大写. 2,字段.方法以及对象(句柄)的首字母应小写. 3,对于所有标识符,其中包含的所有单词都应紧靠在一起,而且大写中间单词的首字 ...

  6. windows下使用Git Bash命令行克隆远程仓库代码

    此处使用的代码托管平台是GitLab,相比GitHub来说,它可以设置免费的私有仓库,哈哈,妈妈再也不用担心我的源码泄露了!1.切换到本地的工作目录,我的目录是: cd /d/coder/websit ...

  7. __init__class的简单使用/理解

    # -*- coding: utf-8 -*- class Student(object): def __init__(self, name, score): #通过定义一个特殊的__init__方法 ...

  8. python 调用 R,使用rpy2

    python 与 R 是当今数据分析的两大主流语言.作为一个统计系的学生,我最早接触的是R,后来才接触的python.python是通用编程语言,科学计算.数据分析是其重要的组成部分,但并非全部:而R ...

  9. Mysql编译安装及优化

    采取编译安装的方法,其好处为:编译安装与平台无关,安装的MySQL目录独立,维护起来方便,而且拥有更好的性能. 环境:CentOS release 6.9 (Final)  x86_64 1)下载my ...

  10. 3.移植驱动到3.4内核-移植DM9000C驱动

    在上章-使内核支持烧写yaffs2,裁剪内核并制作补丁了 本章,便开始移植以前2.6内核的驱动到3.4新内核 1.介绍 首先内核更新,有可能会重新定义新的宏,去除以前的宏,以前更改函数名等 所以移植驱 ...