1187B.md (2445B)
1 Source: https://codeforces.com/problemset/problem/1187/B 2 3 B. Letters Shop 4 5 time limit per test 6 7 2 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 The letters shop showcase is a string $s$, consisting of $n$ lowercase Latin letters. As the name tells, letters are sold in the shop. 22 23 Letters are sold one by one from the leftmost to the rightmost. Any customer can only buy some prefix of letters from the string $s$. 24 25 There are $m$ friends, the $i$-th of them is named $t_i$. Each of them is planning to estimate the following value: how many letters (the length of the shortest prefix) would s/he need to buy if s/he wanted to construct her/his name of bought letters. The name can be constructed if each letter is presented in the equal or greater amount. 26 27 * For example, for $s$="arrayhead" and $t_i$="arya" $5$ letters have to be bought ("arrayhead"). 28 * For example, for $s$="arrayhead" and $t_i$="harry" $6$ letters have to be bought ("arrayhead"). 29 * For example, for $s$="arrayhead" and $t_i$="ray" $5$ letters have to be bought ("arrayhead"). 30 * For example, for $s$="arrayhead" and $t_i$="r" $2$ letters have to be bought ("arrayhead"). 31 * For example, for $s$="arrayhead" and $t_i$="areahydra" all $9$ letters have to be bought ("arrayhead"). 32 33 It is guaranteed that every friend can construct her/his name using the letters from the string $s$. 34 35 Note that the values for friends are independent, friends are only estimating them but not actually buying the letters. 36 37 Input 38 39 The first line contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of showcase string $s$. 40 41 The second line contains string $s$, consisting of exactly $n$ lowercase Latin letters. 42 43 The third line contains one integer $m$ ($1 \le m \le 5 \cdot 10^4$) — the number of friends. 44 45 The $i$-th of the next $m$ lines contains $t_i$ ($1 \le |t_i| \le 2 \cdot 10^5$) — the name of the $i$-th friend. 46 47 It is guaranteed that $\sum \limits_{i=1}^m |t_i| \le 2 \cdot 10^5$. 48 49 Output 50 51 For each friend print the length of the shortest prefix of letters from $s$ s/he would need to buy to be able to construct her/his name of them. The name can be constructed if each letter is presented in the equal or greater amount. 52 53 It is guaranteed that every friend can construct her/his name using the letters from the string $s$. 54 55 Example 56 57 Input 58 59 ``` 60 9 61 arrayhead 62 5 63 arya 64 harry 65 ray 66 r 67 areahydra 68 ``` 69 70 Output 71 72 ``` 73 5 74 6 75 5 76 2 77 9 78 ```