#K81027. Nearest Occurrence Index
Nearest Occurrence Index
Nearest Occurrence Index
Given a string \(S\) and a number of queries \(Q\), your task is to determine, for each query character, the index of its first occurrence in \(S\). If the character does not appear in \(S\), output \(-1\).
For a query character \(c\), you need to compute:
[ ans = \begin{cases} \min{ i \mid S[i] = c } & \text{if } c \text{ appears in } S,\ -1 & \text{otherwise.} \end{cases} ]
The result for each test case should be printed on a separate line with the answers for each query separated by a space.
inputFormat
The first line of input contains an integer (T), representing the number of test cases. For each test case, the input is structured as follows:
- A single line containing the string (S).
- A single line containing an integer (Q), the number of queries.
- (Q) subsequent lines, each containing a single character query.
All input is read from standard input (stdin).
outputFormat
For each test case, output one line containing (Q) integers separated by a space. Each integer is the index of the first occurrence of the corresponding query character in (S), or (-1) if the character does not exist. Output to standard output (stdout).## sample
2
abcde
3
a
b
z
aabbcc
4
a
d
b
c
0 1 -1
0 -1 2 4
</p>