All problems

Problem 529

Longest Word in Dictionary through Deleting

Medium · 20 pts · String, Two Pointers, Sorting

Problem

You are given a string S and a dictionary of N words. Find the longest dictionary word that can be formed by deleting some characters of S, that is, a word that is a subsequence of S. If several words have that longest length, print the lexicographically smallest of them. If no dictionary word can be formed, print -1. Check each word with two pointers and keep the best one.

Input

The first line contains S. The second line contains N. The third line contains N space-separated words.

Output

Print the chosen word, or -1.

Constraints

1 <= |S| <= 1000; 1 <= N <= 1000; 1 <= |word| <= 1000; all lowercase letters

Example input

abpcplea
4
ale apple monkey plea

Example output

apple

Explanation

With S = abpcplea, the words ale, apple and plea can all be formed. apple is the longest of them, with 5 letters, so the answer is apple.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Number of Matching Subsequences Next: Optimal Partition of String