codeforce round#466(div.2)C. Phone Numbers

C. Phone Numbers
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
And where the are the phone numbers?

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

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

String p is lexicographically smaller than string q, if p is a prefix of q, is not equal to q or there exists i, such that pi?<?qi and for all j?<?i it is satisfied that pj?=?qj. For example, abc is lexicographically smaller than abcd , abd is lexicographically smaller than abec, afa is 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 of s and the required length of t.

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

Output
Output the string t conforming to the requirements above.

It‘s guaranteed that the answer exists.

Examples
inputCopy
3 3
abc
output
aca
inputCopy
3 2
abc
output
ac
inputCopy
3 3
ayy
output
yaa
inputCopy
2 3
ba
output
baa
Note
In the first example the list of strings t of length 3, such that the set of letters of t is a subset of letters of s is as follows: aaa, aab, aac, aba, abb, abc, aca, acb, .... Among them, those are lexicographically greater than abc: aca, acb, .... Out of those the lexicographically smallest is aca.

  题目大意:给一个长度为n的字符串S,输出一个大于S的字典序的字符串中字典序最小的长度为k的字符串(考试的时候硬是没看懂T.T一直以为输出字典序最小的字符串)

  分析:如果k<=n只用从后往前赋值,如果可以找到一个比该位字符字典序大,ans[i]=x,该位前面的直接等于ans[j]=s[j](j=i-1,j>=0,j--)即可,否则,ans[i]=min(s)(字符串s中最小的字母);

  如果k>n,i<=n时,ans[i]=s[i];i>n,ans[i]=min(s);

  

#define debug
#include<stdio.h>
#include<math.h>
#include<cmath>
#include<queue>
#include<stack>
#include<string>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<functional>
#include<iomanip>
#include<map>
#include<set>
#define pb push_back
using namespace std;
typedef long long ll;
pair<ll,ll>PLL;
pair<int,ll>Pil;
const int INF = 0x3f3f3f3f;
const double inf=1e8+100;
const ll maxn =1e5+100;
const int N = 1e4+10;
const ll mod=1000007;
vector<int>v;
char s[maxn],ans[maxn];
int n,k;
bool flag;
void solve() {
	int i,j,t=1;
	//cin>>t;
	while(t--) {
		flag=0;
		vector<int>::iterator it;
		cin>>n>>k>>s;
	//	cout<<n<<" "<<s<<" "<<k<<endl;
		for(i=0; i<n; i++) {
			v.pb(s[i]-‘a‘);
		}
		sort(v.begin(),v.end());
		v.erase(unique(v.begin(),v.end()),v.end());
		for(i=k-1; i>=0; i--) {
			if(k<=n) {
				if(flag){
					ans[i]=s[i];
					continue;
				}
				int t=s[i]-‘a‘;
				it=upper_bound(v.begin(),v.end(),t);
				if(it==v.end()) {
					ans[i]=v[0]+‘a‘;
			//		cout<<ans[i]<<" k<=n ";
				} else {
					ans[i]=*it+‘a‘;
			//		cout<<ans[i]<<" k<=n ";
					flag=1;
				}
			} else {
				if(flag) {
					ans[i]=s[i];
			//		cout<<ans[i]<<" k>n ";
				} else {
					for(;i>=n;i--){
						ans[i]=v[0]+‘a‘;
					//	cout<<ans[i]<<" k>n ";
					}
					flag=1;
					i++;//这里不要写落了
				}
			}
		}
	//	cout<<endl;
		cout<<ans<<endl;
	//	v.clear();
	//	memset(ans,‘\0‘,sizeof(ans));
	}
}

int main() {
	ios_base::sync_with_stdio(false);
#ifdef debug
	freopen("in.txt", "r", stdin);
//	freopen("out.txt","w",stdout);
#endif
	cin.tie(0);
	cout.tie(0);
	solve();
	return 0;
}

  

原文地址:https://www.cnblogs.com/visualVK/p/8469206.html

时间: 2024-11-10 07:53:11

codeforce round#466(div.2)C. Phone Numbers的相关文章

codeforce round#466(div.2)B

B. Our Tanya is Crying Out Loud time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Right now she actually isn't. But she will be, if you don't solve this problem. You are given integers n, k, A an

构造 Codeforces Round #107 (Div. 2) B. Phone Numbers

题目传送门 1 /* 2 构造:结构体排个序,写的有些啰嗦,主要想用用流,少些了判断条件WA好几次:( 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <cmath> 8 #include <vector> 9 #include <map> 10 #include <iostream> 11 #include &

Codeforces Round #466 (Div. 2) Problem A - E

从这里开始 题目列表 小结 Problem A Points on the line Problem B Our Tanya is Crying Out Loud Problem C Phone Numbers Problem D Alena And The Heater Problem E Cashback Problem F Machine Learning (Lazy Tag) 小结 这场比赛和同学一起打.本来应该是很开心的事情,结果留下好多遗憾. 第一个遗憾是没能在20分钟内消灭A.B题

Codeforces Round #466 (Div. 2)

所有的题目都可以在CodeForces上查看 中间看起来有很多场比赛我没有写了 其实是因为有题目没改完 因为我不想改,所以就没有写了(大部分题目还是改完了的) 我还是觉得如果是打了的比赛就一场一场写比较好 要不然以后就写有难度的.比较好的题目?? 这场比赛时间真心良心(只是没吃饭) 状态也很好,考场上把\(ABCDE\)切了 \(F\)是自己弃疗了... 不管啦不管啦,进入正题了 题解 A.Points on the line 翻译: 给定一个长度为\(n\)的数组 问最少删去几个数之后使得最大

小蒟蒻初次CF滚粗+爆炸记 (Codeforces Round #466 Div.2)

比赛链接:http://codeforces.com/blog/entry/57981 小蒟蒻今天初次在ZCDHJ张大佬的带领下,打了一场CF (张大佬cnblogs链接:https://www.cnblogs.com/ZCDHJ)' 英文完全看不懂,后面几题直接放弃,各位dalao请见谅 T1: 题目链接:http://codeforces.com/contest/940/problem/A 题目大意: 给你一个n个数的集合,要求你删掉若干数,其中最大的差不应该超过d,求最小删除量. (小蒟蒻

「知识学习&amp;日常训练」莫队算法(一)(Codeforce Round #340 Div.2 E)

题意 已知一个长度为\(n\)的整数数列\(a[1],a[2],-,a[n]\),给定查询参数\(l,r\),问\([l,r]\)内,有多少连续子段满足异或和等于\(k\). 也就是说,对于所有的\(x,y (l\le x\le y\le r)\),能够满足\(a[x]\oplus a[x+1]\oplus ...\oplus a[y]=k\)的\((x,y)\)有多少组. 分析 对于这种离线区间的查询问题(不涉及对区间的更改),我们可以使用莫队算法解决.这类问题是什么类型?对于序列上的区间询问

codeforce Round #599(Div.2)

题目传送门 A. Maximum Square 题目意思是给你n个长条,每个长条的高度是num[i](0 < i < n),每一条的宽度都是 1 :然后求这些长条可以组成的最大面积的正方形的宽度是多少,将它输出来. 题目分析 :因为要求的是正方形,而且面积是最大的,所以既要看它的宽度也要看它的高度.因为多余的面积可以删除掉,所以我们只需要知道min(长,宽). 所以可以将所有的长度排个序,然后从大到小历经一遍就可以了. 1 #include <iostream> 2 #includ

codeforce Round #605(Div.3)

A. Three Friends 题目链接:http://codeforces.com/contest/1272/problem/A 题目意思:有三个好朋友a,b,c.他们在一个坐标轴上,他们的位置分别是xa 和xb ,xc,他们三个人都可以往前或者往后走一步,只能走一步.问你他们走了(也可能不走)之后的abs(xa  -  xb) + abs(xb - xc) + abs(xc - xa )的最小值是多少? 题目分析:我们可以假定朋友a的坐标最小,朋友b比朋友a更大,但是又比朋友c更小,所以朋

Codeforces Round #466 (Div. 2) F - Machine Learning

可以观察到,因为我们答案是通过MEX函数得到,那么假设,当前MEX的值是p,那么这时候一共有1+2+3+4...+p-1个与\(c_1\),\(c_2\),\(c_3\)...\(c_p-1\)相同,一共是p*(p-1)/2个数字,那么MEX值一定不超过\(\sqrt n\).故暴力能统计答案. 对于有修改的询问,我们采用待修改莫队,取块的大小为\(n^{\frac23}\). #include<iostream> #include<cstring> #include<alg