CodeForces - 940C(模拟)

本文介绍了一种算法,用于从给定字符串中构建长度为k的新字符串,该字符串的字母集合是原字符串的子集,同时确保新字符串字典序大于原字符串,并在所有符合条件的字符串中字典序最小。

And where the are the phone numbers?

You are given a string s consisting of lowercase English letters and an integerk. Find the lexicographically smallest stringt of length k, such that its set of letters is a subset of the set of letters ofs and s is lexicographically smaller thant.

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 ofabadaba is {a, b, d}.

String p is lexicographically smaller than stringq, if p is a prefix ofq, is not equal to q or there exists i, such thatpi < qi and for allj < i it is satisfied that pj = qj. For example,abc is lexicographically smaller than abcd , abd is lexicographically smaller thanabec, afais 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 ofs and the required length of t.

The second line of input contains the string s consisting ofn lowercase English letters.

Output

Output the string t conforming to the requirements above.

It's guaranteed that the answer exists.

Example
Input
3 3
abc
Output
aca
Input
3 2
abc
Output
ac
Input
3 3
ayy
Output
yaa
Input
2 3
ba
Output
baa
Note

In the first example the list of strings t of length 3, such that the set of letters oft is a subset of letters of s is as follows: aaa, aab, aac, aba, abb, abc, aca, acb, .... Among them, those are lexicographically greater thanabc: aca,acb, .... Out of those the lexicographically smallest isaca.

题意:给你一串长度为n的字符串,要求在原字符串中挑出出现过的字母组成长度为k的字符串且字典序大于原字符串,并且要求是所有满足条件的字符串中的字典序最小的字符串。

只需将原字符串的前k个字符串进行操作,取出前k个字符串,并询问第k个字母是否为出现过的字母中最大的,如果是向前询问直到那个字母不是最大的,然后将前面的字符串原样输出,将那个不是最大的字母换成出现过的字母的比他大一点的字母,之后的字母全换成最小的就行了。说得有点乱,看代码吧。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<math.h>
#include<iostream>
#include<queue>
#include<map>
#include<vector>
#include<stack>
#define inf 0x3fffffff
using namespace std;
typedef long long LL;
const int N=1e5+1;
char s[N];
bool vis[30];
int main()
{
    int n,k,mina,maxa;
    while(~scanf("%d%d",&n,&k))
    {
        mina=30,maxa=0;
        memset(vis,false,sizeof(vis));
        scanf("%s",s);
        for(int i=0;s[i]!='\0';i++)
        {
            vis[s[i]-'a']=true;
            mina=min(mina,s[i]-'a');
            maxa=max(maxa,s[i]-'a');
        }
        if(k>n)
        {
            printf("%s",s);
            for(int i=n;i<k;i++)
                printf("%c",mina+'a');
            printf("\n");
        }
        else
        {
            for(int i=k-1;i>=0;i--)
            {
                if(s[i]-'a'!=maxa)
                {
                    for(int j=0;j<i;j++)
                        printf("%c",s[j]);
                    for(int j=0;j<26;j++)
                    {
                        if(vis[j]&&j>s[i]-'a')
                        {
                            printf("%c",j+'a');
                            break;
                        }
                    }
                    for(int j=0;j<k-1-i;j++)
                        printf("%c",mina+'a');
                    printf("\n");
                    break;
                }
            }
        }
    }
}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值