列表

详情


NC232091. Lexicographic Order

描述

Nana7mi has a string s with length of n and containing only lowercase characters.
Now she wants to know the lexicographically greatest string t with length not exceed m, containing only lowercase characters and is lexicographically less than s.
String x is lexicographically less than string y, if either x is a prefix of y (and ), or there exists such ), that , and for any . Here denotes the length of the string a.


输入描述

The first line of input contains two integers  --- the length of s and the length limit of t.
The second line contains a string s with length of n, as the string Nana7mi has.

输出描述

Output a string with length not exceed m and containing only lowercase characters in a line.

示例1

输入:

3 4
ybb

输出:

ybaz

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C 解法, 执行用时: 7ms, 内存消耗: 3228K, 提交时间: 2021-12-18 16:59:02

#include<stdio.h>
char a[1000005];
char b[1000005];
int main(){
	int n,m,i;
	scanf("%d %d",&n,&m);
	scanf("%s",a);
	for(i=0;i<n-1;i++){
		b[i]=a[i];
	}
	if(a[i]!='a'){
		b[i]=a[i]-1;
		for(i=i+1;i<m;i++){
			b[i]='z';
		}
	}
	printf("%s",b);
	return 0;
} 

C++ 解法, 执行用时: 27ms, 内存消耗: 2496K, 提交时间: 2021-12-19 16:03:46

#include<bits/stdc++.h>
using namespace std;
int n,m;
string s;
int main () {
	cin>>n>>m>>s;
	if (s.back()=='a') s.pop_back();
	else {
		s.back()--;
		while (s.size()<m) s.push_back('z');
	}
	cout<<s;
}

pypy3 解法, 执行用时: 96ms, 内存消耗: 27612K, 提交时间: 2022-11-03 15:04:04

n,m=map(int,input().split())
s=input()
if s[n-1]=="a":
    print(s[:-1])
else:
    print(s[:-1]+chr(ord(s[n-1])-1)+"z"*(m-n))

上一题