The Substring Game!
Practice
4.3 (8 votes)
Algorithms
Binary search
Hiring
Implementation
Medium
Searching
String manipulation
Problem
32% Success 4989 Attempts 30 Points 1.5s Time Limit 256MB Memory 1024 KB Max Code

Let's play the Substring Game.

This game has two characters: Sherlock and Watson. As usual, Watson wants to test the skills of Mr. Sherlock.

Watson gives Sherlock a string (let's denote it by S). Watson calculates all the substrings of S in his favourite order.

According to the Watson's favourite order, all the substrings starting from first character of the string will occur first in the sorted order of their length, followed by all the substrings starting from the second character of the string in the sorted order of their length, and so on.

For example:
Suppose the given string is "abc" that is S = abc. Then, all the substrings of S as per Watson's favourite order are:

1) a

2) ab

3) abc

4) b

5) bc

6) c

The game is pretty simple.

Watson will ask Sherlock 'q' questions. In each question, Watson will give Sherlock a number and Sherlock has to speak the substring on that number. If there is no possible substring, then Sherlock has to speak -1.

Note: Sherlock has to follow Watson's favourite order.

Help Sherlock.

See Sample test-case for more understanding.


Input format:

First line consist of a string 'S', having only the small alphabets (a - z).

Second Line consist of an integer 'q', number of questions Watson ask.

Next line consists of 'q' space-separated numbers.


Output format:

For all the 'q' questions, output the required answer, in a new line.


Constraints:

1 <= |S| <= 10^5

1 <= q <= 10^3

1 <= number <= 10^18

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
6 votes
Tags:
AlgorithmsApprovedBinary SearchMediumOpenSorting
Points:30
18 votes
Tags:
AlgorithmsBinary SearchGreatest common divisorMediumSearchinggcd
Points:30
8 votes
Tags:
AlgorithmsBinary Search