Autocomplete CodeForces - 53A

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.

Example
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(为前缀字符),给定一个n,输出n个字符串,若字符前缀为s,则输出有前缀s的字典序的最小值,若一个也不存在,则输出s


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int M=110;
char s[M][M];
char a[M];
int f[M],t[M];
char b[M];
int main()
{
    int n;
    while(cin>>a)
    {
        cin>>n;
        int c=0;
        memset(f,0,sizeof(f));
        memset(t,0,sizeof(t));
        int len=strlen(a);
        for(int i=0;i<n;i++)
        {
            cin>>s[i];
            int len1=strlen(s[i]);
            if(len1<len)
            {
                t[i]=1;
                continue;
            }
            for(int j=0;j<len;j++)
            {
                if(a[j]!=s[i][j])
                {
                    t[i]=1;
                    break;
                }
            }
            if(t[i]==0)
            {
                 f[i]=1;
                memset(b,0,sizeof(b));
                strcpy(b,s[i]);
                c++;
            }
        }
        if(c==0)
            cout<<a<<endl;
        else
        {
            for(int i=0;i<n;i++)
            {
                if(f[i]==1)
                {
                    if(strcmp(b,s[i])>0)
                    {
                       memset(b,0,sizeof(b));
                       strcpy(b,s[i]);
                    }
                }
            }
            cout<<b<<endl;
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值