Codeforces Beta Round #49 (Div. 2)—A

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 prefixs.

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.

注意:不是要求长度最小,而是按字典序最小

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#define maxn 120
using namespace std;
struct node
{
    char address[maxn];
}a[maxn];
bool flag[maxn]={false};
int main()
{
    char tar[maxn];
    cin>>tar;
    int n;
    cin>>n;
    int len=strlen(tar);
        for(int i=0;i<n;i++) cin>>a[i].address;
    for(int i=0;i<n;i++)
        for(int j=0,p=0;j<len;j++,p++)
        {
            if(tar[j]!=a[i].address[p]) flag[i]=true;
        }
    int idx;
    bool check=false;
    for(int i=0;i<n;i++)
        if(!flag[i])
        {
            idx=i;
            check=true;
            break;
        }
    for(int j=idx+1;j<n;j++) if(!flag[j]&&strcmp(a[idx].address,a[j].address)>0) idx=j;
    if(!check) cout<<tar;
    else cout<<a[idx].address;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值