問題一覧 > 通常問題

No.2020 Sum of Common Prefix Length

レベル : / AI推定難易度 : 1900 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 34
作問者 : souta-1326 / テスター : MtSaka
お気に入りにしたユーザー ProblemId : 8010 / 自分の提出
問題文最終更新日: 2022-07-22 20:38:18
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 353 (オムニバス) (順位表) の他の問題:

Note

For this problem, the intended solution gets TLE in Python3, and even in PyPy3 it is difficult without sufficient optimization ( $867\mathrm{ms}$ ).
Using a fast language such as C++ is recommended.

Problem Statement

You are given $N$ strings, and the $i$-th string is denoted by $S_i$.
Process $Q$ queries in order. Each query is one of the following $2$ types.

  • 1 x c: Append the character $c$ to the end of $S_x$.
  • 2 x: For each $k\ (1 \leq k \leq N)$, find the length of the longest common prefix of $S_x,S_k$, and answer the sum of these lengths.

Input

$N$
$S_1$
$S_2$
$\vdots$
$S_N$
$Q$
$\mathrm{query}_1$
$\mathrm{query}_2$
$\vdots$
$\mathrm{query}_Q$
Each query is given in one of the following forms.
$1\ \ x\ \ c$
$2\ \ x$

  • $1 \leq N,Q \leq 2\times 10^5$
  • $N,Q$ are integers
  • $S_i$ is a string of at least $1$ character consisting of lowercase English letters
  • $\displaystyle \sum_{i=1}^N |S_i| \leq 2\times 10^5$ (before processing the queries)
  • $1 \leq x \leq N$
  • $c$ is a lowercase English letter
  • There is at least $1$ query of the $2$nd type

Output

For all queries of the $2$nd type, output the answer in order, each followed by a newline.

Sample

Sample 1
Input
3
a
abc
abcde
4
1 1 b
2 2
1 2 d
2 3
Output
8
11

Initially, $S = (\mathrm{a,abc,abcde})$.
In the $1$st query, $\mathrm{b}$ is appended to the end of $S_1$, and $S_1 = \mathrm{ab}$.
In the $2$nd query, output the sum of the lengths of the longest common prefixes of $S_2$ and each string. Here, output $2+3+3=8$.
In the $3$rd query, $\mathrm{d}$ is appended to the end of $S_2$, and $S_2 = \mathrm{abcd}$.
In the $4$th query, output the sum of the lengths of the longest common prefixes of $S_3$ and each string. Here, output $2+4+5=11$.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。