【String数组排序】#49 A. Autocomplete

A. Autocomplete
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Autocomplete is a program function that enables inputting the text (in editors, command line shells, browsers etc.) completing the text by its inputted part. Vasya is busy working on a new browser called 'BERowser'. He happens to be working on the autocomplete function in the address line at this very moment. A list consisting of n last visited by the user pages and the inputted part s are known. Your task is to complete s to make it an address of one of the pages from the list. You have to find the lexicographically smallest address having a prefix s.

Input

The first line contains the s line which is the inputted part. The second line contains an integer n (1 ≤ n ≤ 100) which is the number of visited pages. Then follow n lines which are the visited pages, one on each line. All the lines have lengths of from 1 to 100 symbols inclusively and consist of lowercase Latin letters only.

Output

If s is not the beginning of any of n addresses of the visited pages, print s. Otherwise, print the lexicographically minimal address of one of the visited pages starting from s.

The lexicographical order is the order of words in a dictionary. The lexicographical comparison of lines is realized by the '<' operator in the modern programming languages.

Sample test(s)
input
next
2
nextpermutation
nextelement
output
nextelement
input
find
4
find
findfirstof
findit
fand
output
find
input
find
4
fondfind
fondfirstof
fondit
fand
output
find

问能够把字符串s补齐的字符串中字典序最小的是哪个

用substr来分析这个字符串是否是以s为前缀的,然后用string数组来存。

最后stl的sort字典序排序之后输出头。

这题我用了string数组排序,需要注意的是stl的sort来排序的话起始位置为起始位置,但结束位置为终止位置加一

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
string us[1001];

int main()
{
	string s;	cin>>s;
	int n;	cin>>n;	
	char huiche;
	scanf("%c",&huiche);
	int len=s.length(),idx=0;
	for(int i=0;i<n;i++)
	{
		string stmp; cin>>stmp;
		//cout<<stmp.substr(0,len);
		if(stmp.length()<len);
		else if(stmp.substr(0,len)==s) 
		{
			us[++idx]=stmp;
			//cout<<idx;
		}
	}
	if(!idx)cout<<s<<endl;
	else
	{
		sort(us+1,us+idx+1);	//begin & end+1
		//for(int i=1;i<=idx;i++)	cout<<i<<":"<<us[i]<<endl;
		cout<<us[1]<<endl;
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糖果天王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值