Search Engine
Practice
4.1 (44 votes)
Medium
String manipulation
Trees
Tries
Problem
45% Success 4983 Attempts 30 Points 2s Time Limit 1024MB Memory 1024 KB Max Code

Let us see how search engines work. Consider the following simple auto complete feature. When you type some characters in the text bar, the engine automatically gives best matching options among it's database. Your job is simple. Given an incomplete search text, output the best search result.

Each entry in engine's database has a priority factor attached to it. We consider a result / search suggestion best if it has maximum weight and completes the given incomplete search query. For each query in the input, print the maximum weight of the string in the database, that completes the given incomplete search string. In case no such string exists, print -1.

INPUT

First line contains two integers n and q, which represent number of database entries and number of search queries need to be completed. Next n lines contain a string s and an integer weight, which are the database entry and it's corresponding priority.

Next q lines follow, each line having a string t, which needs to be completed.

OUTPUT

Output q lines, each line containing the maximum possible weight of the match for given query, else -1, in case no valid result is obtained.

CONSTRAINTS

1 ≤ n, weight, len(s), len(t) ≤ 106
1 ≤ q ≤ 105
total length of all strings in database entries ≤ 106
total length of all query strings ≤ 106

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
10 votes
Tags:
ApprovedData StructuresMediumOpenTreesTries
Points:30
10 votes
Tags:
TrieAdvanced Data StructuresData Structures
Points:30
15 votes
Tags:
Binary SearchData StructuresMediumTries