Phone Numbers

本文介绍了一种算法,用于生成一个长度为k的字符串t,该字符串的字母集合是给定字符串s的子集,并且t在字典序上大于s且尽可能小。文章通过贪心策略解决了这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

C. Phone Numbers

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 notlexicographically 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 oft.

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

input

Copy

3 3
abc

output

aca

input

Copy

3 2
abc

output

ac

input

Copy

3 3
ayy

output

yaa

input

Copy

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.

有道翻译:

你给出一个字符串s由小写英文字母和一个整数k。找到按最小的字符串长度t的k,这样它的字母的一个子集组字母s和s按小于t。

保证答案的存在。注意字母的集合是一组,不是一个多重集。例如,一组字母abadaba是{ a,b,d }。p是按小于字符串的字符串,如果p是一个前缀,不等于q或存在,这样pi < qi,对于所有j < i都满足pj = qj。例如,abc字母角度小于abcd abd比abec小字母顺序,afa在字典上比ab小,而a在字典上比a小。

 

输入输入的第一行包含两个整数n和k(1≤n,k≤100)- s和t的所需长度的长度。第二行输入包含字符串的s n小写英文字母组成。输出输出字符串t符合上面的要求。保证答案的存在。

 

就是说,只能用s的集合里的字符,组成长度为k的字符串t。t要大于s且要求t最小

 

我再一次想到贪心,分两种情况。

1.k>n那么只要前面和s一样后面k-n个都是集合中最小的字符

2.k<=n。因为字典序中字符串大小先是由下标小的决定的。所以尽量保证前面不变。后面往前找,第一个找到能被替换掉的字符(即集合中有字符大于这个字符)时,前面不变这个变成大于这个字符且最小的字符,后面全是集合中最小的字符。

#include<bits/stdc++.h>
using namespace std;
bool w[30];
int main()
{
	int n,k,i,j,l,r;
	string s;
	cin>>n>>k;
	cin>>s;
	for(i=0;i<s.size();i++)
		w[s[i]-'a'+1]=1;	    //保存字符 
	for(i=1;i<=26;i++)		    //找到集合中最小字符,l为其ASCII码 
		if(w[i]==1){
			l=i+'a'-1;
			break;
		}
	for(i=26;i>=1;i--)		    //找到集合中最大字符,r为其ASCII码 
		if(w[i]==1){
			r=i+'a'-1;
			break;
		}
	if(k>n){			    //第一种情况 
		cout<<s;   
		for(i=1;i<=k-n;i++)
			cout<<char(l);
		return 0;
	}
	for(i=k-1;i>=0;i--){
		if(r>s[i]){		    //如果有r大于s[i],那么集合中肯定有字符大于s[i] 
			cout<<s.substr(0,i);//输出前面 
			for(j=s[i]-'a'+2;j<=26;j++){
				if(w[j]){    //找到第一个大于s[i],的字符输出 
					cout<<char(j+'a'-1);
					break;
				}
			}
			for(j=i+1;j<k;j++)    //输出后面 
				cout<<char(l);
			return 0;
		}
	}
	return 0;
}

 

<think>好的,我需要回答用户关于phonenumbers库的使用说明、示例和介绍。首先,我得整理已有的信息,特别是用户提供的四个引用内容。 引用1提到了PhoneNumberOfflineGeocoder和PhoneNumberToCarrierMapper,这两个类分别用于获取地理位置和运营商信息。引用2展示了仓颉三方库中使用phonenumber4cj的代码示例,包括解析号码和获取类型。引用3提到数字签名的示例仅用于演示,但这里可能不相关。引用4则推荐了Python-Phonenumbers项目,并给出了项目地址,说明它是Google的libphonenumber的Python移植。 用户主要想了解phonenumbers库,所以重点应放在Python版本上,即引用4提到的python-phonenumbers。需要介绍库的功能,比如解析、验证、格式化电话号码,以及获取地理位置和运营商信息。然后给出安装方法,比如pip install phonenumbers。接着是使用示例,比如解析号码、验证有效性、格式化输出、获取地理位置和运营商。这些示例可以参考引用2中的代码结构,但用Python语法改写。例如,解析号码后使用geocoder和carrier模块获取信息。同时,要提到国际号码需要带国家代码,并且不同国家可能有不同的验证规则。最后,给出相关项目地址,如引用4中的链接。 可能还需要补充一些注意事项,比如处理号码时的常见错误,或者性能优化,如引用3提到的压缩和优化代码,不过这部分可能不直接相关。要确保示例代码正确,符合Python语法,比如正确导入模块,调用函数的方法。 最后,生成相关问题需要考虑用户可能进一步想知道的内容,比如如何验证号码、处理国际号码、获取更多信息的方法,以及与其他库的比较或实际应用场景。</think>### phonenumbers库介绍及使用说明 phonenumbers是Google libphonenumber库的Python移植版本,用于解析、验证和格式化国际电话号码,支持获取号码地理位置、运营商等信息[^4]。 #### 核心功能 1. 号码解析与验证 2. 格式标准化输出 3. 地理位置信息查询 4. 运营商信息识别 5. 号码类型判断(固话/手机/免费号码等) --- ### 安装方法 ```bash pip install phonenumbers ``` --- ### 基础使用示例 #### 1. 号码解析与验证 ```python import phonenumbers # 解析电话号码 phone = phonenumbers.parse("+8613812345678", None) # 必须包含国家代码 # 验证有效性 print(phonenumbers.is_valid_number(phone)) # 输出: True # 获取国家代码 print(phone.country_code) # 输出: 86 ``` #### 2. 格式标准化 ```python # 转换为国际标准格式 print(phonenumbers.format_number(phone, phonenumbers.PhoneNumberFormat.INTERNATIONAL)) # 输出: +86 138 1234 5678 # 转换为本地格式 print(phonenumbers.format_number(phone, phonenumbers.PhoneNumberFormat.NATIONAL)) # 输出: 138 1234 5678 ``` #### 3. 地理位置查询 ```python from phonenumbers import geocoder # 获取中文地理位置描述 location = geocoder.description_for_number(phone, "zh") print(location) # 输出: 中国 ``` #### 4. 运营商识别 ```python from phonenumbers import carrier # 获取运营商信息(需开启数据加载) carrier_name = carrier.name_for_number(phone, "zh") print(carrier_name) # 输出: "中国移动" 或其他运营商 ``` --- ### 高级功能示例 ```python # 获取号码类型 from phonenumbers import PhoneNumberType number_type = phonenumbers.number_type(phone) print("手机号码" if number_type == PhoneNumberType.MOBILE else "其他类型") # 生成号码示例 example_number = phonenumbers.example_number("CN") print(phonenumbers.format_number(example_number, phonenumbers.PhoneNumberFormat.INTERNATIONAL)) ``` --- ### 注意事项 1. 必须包含国家代码(如中国+86) 2. 不同国家/地区的号码验证规则不同 3. 地理位置和运营商数据需要单独加载数据库 4. 支持170+国家/地区的号码处理 项目地址:https://gitcode.com/gh_mirrors/py/python-phonenumbers [^4] ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值