传送门

题目大意

给你两个数字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. 织梦CMS搭建网站必做的服务器相关安全设置

    http://help.dedecms.com/install-use/server/2011/1109/2124.html#printSource http://www.aliweihu.com/9 ...

  2. 用Dedecms5.7的arclist标签调用文章内容

    arclist标签调用文章内容 首先大家都知道在Dedecms中,list标签是可以调用文章内容的,调用格式就不再此冗述了.从我个人来说,我非常不喜欢用list标签调用,有可能我会尽量使用arclis ...

  3. 栏目class导航

    <div id="index_nav"> <div class="index_nav"> <ul> <!-- 调用栏目 ...

  4. Web API (四) 特性路由(Attribute Route)

    特性路由 是Web API 2 中提出的一种新的类型的路由,正如其名称那样,它是通过特性(Attribute) 来定义路由的,相比之前的基于模式(Convertion Based)的路由,特性路由 能 ...

  5. NV12格式转RGB的CUDA实现

    NV12格式是yuv420格式的一种,NV12格式的u,v排布顺序为交错排布,假如一幅图像尺寸为W*H,则先Y分量有W*H个,然后U分量和V分量交错排布,U分量和V分量各有W*H/4个,U,V加起来总 ...

  6. ublime Text 3安装与使用

    ublime Text 3安装与使用 工具 2015-07-30 10:46 0 34 工欲善其事,必先利其器.好的工具帮助我们节省大量的工作时间,好用的插件使工具更强大. 1. 下载 可以从官网 h ...

  7. linux_用户和组

    linux用户分为3类: 超级用户:root, UID为0, GID为0 普通用户: 500 -65535, 由root创建 虚拟用户: 1-499 - 系统里傀儡,不能使用,固定存在,满足linux ...

  8. python_如何让字典保持有序?

    案例: 某编程竞赛系统,对参赛选手编程解题进行计时,选手完成题目后,把该选手解题用时记录到字典中,以便赛后按选手名查询成绩(答题时间越短,成绩越优秀) {'lili':(1,43),'yuyu':(2 ...

  9. CColor类封装

    CColor类封装 Color.h #pragma once #include <sstream> #include <string> using namespace std; ...

  10. typedef和#define的简单比较

    1.通常说typedef比#define要好,尤其在有指针的情况下 typedef char* pStr1; #define pStr2 char* pStr1 s1,s2; pStr2 s3,s4; ...