Counting numbers

2.7

9 votes
Searching algorithm, Binary Search, Algorithms, Recursion
Problem

A number B is a subset of number A if you can delete some digits from number A to the remaining digits from number B.

You are given an N-digit number X and XK is the largest K-digit subtype of the number X.

You must answer M queries. Each query consists of two numbers K and L. The answer to the query is the Lth digit of the number XK.

Input format

  • The first line contains an integer X of length N.
  • The second line contains number M.
  • The next M lines contain two numbers Ki and Li denoting the parameters of the ith query.

Output format

The output must contain one line of length M in which the ith character is a response to the ith query.

Constraints

1N1051M1051KiN1LiKi

Sample Input
31415926
7
2 2
3 1
1 1
4 3
5 2
8 2
7 3
Sample Output
6992511
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In first queries subnumber is 96, second digit = 6.

In second queries subnumber is 926, first digit = 9.

Editor Image

?