Autocomplete (Codeforces 53A)
Tags: Implement, implementation

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 Inputs:
next
2
nextpermutation
nextelement

find
4
find
findfirstof
findit
fand

find
4
fondfind
fondfirstof
fondit
fand

Sample Outputs:
nextelement

find

find

Solution (Python):
search_string = input()
number_of_lines = int(input())
addresses = [input() for _ in range(number_of_lines)]
addresses.sort()
for address in addresses:
    if address.startswith(search_string):
        print(address)
        break
else:
    print(search_string)

Solution (Python):
def main():
    s = input()
    pages = sorted([input() for i in range(int(input()))])
    for page in pages:
        if page.startswith(s):
            print(page)
            return
    print(s)
if __name__ == '__main__':
    main()

Solution (Python):
a = input()
c = 'z' * 100
n = ''
for i in range(int(input())):
    b = input()
    if a == b[:len(a)] and b <= c:
        n = b
        c = b
if len(n) > 0:
    print(n)
else:
    print(a)