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.

 

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

 

 这道题没人写博客,很神奇啊,所以我就写了……

一开始没想到要怎么搞,后来看别人的代码发现一个string就可以解决……

很忧郁

 

 

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    string c;
    string ans="";
    int n;
    cin>>s;
    cin>>n;
    
    for(int i=0;i<n;i++)
    {
        bool mark=true;
        cin>>c;
        for(int j=0;j<s.size();j++)
        if(s[j]!=c[j])
        mark=false;
        if(mark)
        {
            if(ans==""||ans>c)
            ans=c;
        }
    }
    if(ans=="")
    cout<<s<<endl;
    else
    cout<<ans<<endl;
    return 0;
 } 

 

posted @ 2017-08-14 10:26  时荣  阅读(304)  评论(0编辑  收藏  举报