#abc280h. Ex - Substring Sort

Ex - Substring Sort

Score : 600600 points

问题描述

给定 NN 个字符串 S1,S2,,SNS_1, S_2, \ldots, S_N。令 $M = \displaystyle\sum_{i=1}^N \frac{|S_i|(|S_i|+1)}{2}$。

对于一个字符串 SS 和整数 LLRR,我们用 S[L,R]S[L, R] 表示由第 LL 个到第 RR 个字符组成的子串。

考虑一个长度为 MM 的三元组整数序列 $((K_1, L_1, R_1), (K_2, L_2, R_2), \ldots, (K_M, L_M, R_M))$,它满足以下条件:

  • MM 个元素两两不同。
  • 对于所有 1iM1 \leq i \leq M,有 1KiN1 \leq K_i \leq N1LiRiSKi1 \leq L_i \leq R_i \leq |S_{K_i}|
  • 对于所有 1ijM1 \leq i \leq j \leq M,在字典序中满足 SKi[Li,Ri]SKj[Lj,Rj]S_{K_i}[L_i, R_i] \leq S_{K_j}[L_j, R_j]

已知 QQ 个整数 x1,x2,,xQx_1, x_2, \ldots, x_Q,它们介于 11MM(包括两端点)之间。对于每个 1iQ1 \leq i \leq Q,找出 (Kxi,Lxi,Rxi)(K_{x_i}, L_{x_i}, R_{x_i}) 的可能实例。可以证明总存在满足条件的三元组序列。如果有多组满足条件的三元组,输出任意一组即可。此外,不同的 xix_i 对应的满足条件的三元组序列不必相同。

什么是字典序?两个字符串 SSTT 在字典序中满足 STS \leq T 当且仅当以下任一条件成立:

  • ST|S| \leq |T| 并且 S=T[1,S]S = T[1, |S|]
  • 存在 1kmin(S,T)1\leq k\leq \min(|S|, |T|),使得对于所有 1ik11\leq i\leq k-1SSTT 的第 ii 个字符相同,且 SS 的第 kk 个字符在字母表上严格小于 TT 的第 kk 个字符。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

You are given NN strings S1,S2,,SNS_1,S_2,\ldots, S_N. Let $M = \displaystyle\sum_{i=1}^N \frac{|S_i|(|S_i|+1)}{2}$.
For a string SS and integers LL and RR, let us denote by S[L,R]S[L, R] the substring consisting of the LL-th through RR-th characters of SS.

A sequence of triples of integers $((K_1, L_1, R_1), (K_2, L_2, R_2), \ldots, (K_M, L_M, R_M))$ of length MM satisfies the following conditions:

  • The MM elements are pairwise distinct.
  • For all 1iM1 \leq i \leq M, it holds that 1KiN1 \leq K_i \leq N and 1LiRiSKi1 \leq L_i \leq R_i \leq |S_{K_i}|.
  • For all 1ijM1 \leq i \leq j \leq M, it holds that SKi[Li,Ri]SKj[Lj,Rj]S_{K_i}[L_i, R_i] \leq S_{K_j}[L_j, R_j] in the lexicographical order.

You are given QQ integers x1,x2,,xQx_1,x_2,\ldots, x_Q between 11 and MM, inclusive. For each 1iQ1 \leq i \leq Q, find a possible instance of (Kxi,Lxi,Rxi)(K_{x_i}, L_{x_i}, R_{x_i}). We can prove that there always exists a sequence of triples that satisfies the conditions. If multiple triples satisfy the conditions, print any of them. In addition, among different xix_i's, the conforming sequence of triples does not have to be common.

What is the lexicographical order? Two strings SS and TT are said to be STS \leq T in the lexicographical order if and only if one of the following conditions is satisfied:

  • ST|S| \leq |T| and S=T[1,S]S = T[1, |S|].
  • There exists 1kmin(S,T)1\leq k\leq \min(|S|, |T|) such that the ii-th characters of SS and TT are the same for all 1ik11\leq i\leq k-1, and the kk-th character of SS is alphabetically strictly smaller than that of TT.

Constraints

  • 1N1051 \leq N \leq 10^5
  • 1Si1051 \leq \lvert S_i\rvert \leq 10^5
  • $\displaystyle\sum_{i=1}^N \lvert S_i\rvert\leq 10^5$
  • 1Q2×1051 \leq Q \leq 2\times 10^5
  • $1 \leq x_1<x_2<\cdots<x_Q \leq \displaystyle\sum_{i=1}^N \frac{|S_i|(|S_i|+1)}{2}$
  • N,Q,x1,x2,,xQN,Q,x_1,x_2,\ldots,x_Q are integers.
  • SiS_i is a string consisting of lowercase English letters.

Input

The input is given from Standard Input in the following format:

NN

S1S_1

S2S_2

\vdots

SNS_N

QQ

x1x_1 x2x_2 \ldots xQx_Q

Output

Print QQ lines. The ii-th line should contain an instance of conforming (Kxi,Lxi,Rxi)(K_{x_i}, L_{x_i}, R_{x_i}), separated by spaces. If multiple triples satisfy the conditions, print any of them.

Sample Input 1

2
abab
cab
2
5 14

Sample Output 1

1 3 4
2 1 1

We have M=16M=16. One possible sequence of triples that satisfies the conditions is $((1,1,1), (1,3,3), (2,2,2), (1,1,2), (1,3,4), (2,2,3), (1,1,3), (1,1,4), (1,2,2), (1,4,4), (2,3,3), (1,2,3), (1,2,4), (2,1,1), (2,1,2), (2,1,3))$. The corresponding sequence of SKi[Li,Ri]S_{K_i}[L_i, R_i] for these (Ki,Li,Ri)(K_i,L_i, R_i) in this order is (a, a, a, ab, ab, ab, aba, abab, b, b, b, ba, bab, c, ca, cab).

Note that the sequence satisfies the conditions even if we swap the 55-th element (1,3,4)(1,3,4) with the 44-th or 66-th one, so (Kx1,Lx1,Rx1)=(1,1,2),(2,2,3)(K_{x_1}, L_{x_1}, R_{x_1})=(1,1,2), (2,2,3) will also be accepted.

Sample Input 2

3
a
a
ba
2
1 2

Sample Output 2

1 1 1
1 1 1

We have M=5M=5. The sequence of triples that satisfies the conditions can be
(1,1,1),(2,1,1),(3,2,2),(3,1,1),(3,1,2)(1,1,1), (2,1,1), (3,2,2), (3,1,1), (3,1,2) or (2,1,1),(3,2,2),(1,1,1),(3,1,1),(3,1,2)(2,1,1), (3,2,2), (1,1,1), (3,1,1), (3,1,2), for example.

Note that, for (Kxi,Lxi,Rxi)(K_{x_i}, L_{x_i}, R_{x_i}) that you print, the conforming sequence whose xix_i-th element is (Kxi,Lxi,Rxi)(K_{x_i}, L_{x_i}, R_{x_i}) does not have to be common among different ii; in other words, there does not necessarily have to exist a sequence whose "xix_i-th element is (Kxi,Lxi,Rxi)(K_{x_i}, L_{x_i}, R_{x_i}) for all 1iQ1\leq i\leq Q."

Sample Input 3

10
gxgpuamkx
szhkbpphykin
ezplvfja
mopodotkrj
rimlvumuar
nexcfyce
eurgvjyos
dhvuyfvt
nrdyluacvra
ggwnpnzij
6
74 268 310 380 455 489

Sample Output 3

3 1 2
4 4 5
4 3 7
9 6 6
6 6 6
2 2 12

update @ 2024/3/10 11:47:44