G - Edit to Match Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 575575

問題文

NN 個の文字列 S1,S2,,SNS_1,S_2,\ldots,S_N が与えられます。各文字列は英小文字からなります。

k=1,2,,Nk=1,2,\ldots,N に対し以下の問題を解いてください。

T=SkT=S_k として、 TT に対して以下の 22 種類の操作を好きな順番で好きな回数繰り返すことを考える。

  • コストを 11 払い、 TT の末尾の文字を削除する。この操作は TT が空文字列でない時に可能である。
  • コストを 11 払い、 TT の末尾に好きな英小文字を追加する。

TT を空文字列、 S1,S2,,Sk1S_1,S_2,\ldots,S_{k-1} のいずれかと一致させるために払うコストの総和の最小値を求めよ。

制約

  • 1N2×1051\le N\le 2\times 10^5
  • SiS_i は英小文字からなる長さ 11 以上の文字列
  • i=1NSi2×105\displaystyle \sum_{i=1}^N |S_i|\le 2\times 10^5

入力

入力は以下の形式で標準入力から与えられる。

NN
S1S_1
S2S_2
\vdots
SNS_N

出力

NN 行出力せよ。 ii 行目 (1iN)(1\le i\le N) には k=ik=i に対する答えを出力せよ。


入力例 1Copy

Copy
3
snuke
snuki
snuuk

出力例 1Copy

Copy
5
2
4

k=1k=1 の場合は末尾の文字を削除する操作を 55 回行うことで空文字列にすることができます。

k=2k=2 の場合は末尾の文字を削除した後に末尾に e を追加することで S1S_1 と一致させることができます。

k=3k=3 の場合は末尾の文字を 22 回削除した後末尾に k を追加し、末尾に i を追加することで S2S_2 と一致させることができます。


入力例 2Copy

Copy
3
abc
arc
agc

出力例 2Copy

Copy
3
3
3

入力例 3Copy

Copy
8
at
atatat
attat
aatatatt
attattat
ttatta
tta
tt

出力例 3Copy

Copy
2
4
3
8
3
6
3
1

Score : 575575 points

Problem Statement

You are given NN strings S1,S2,,SNS_1,S_2,\ldots,S_N. Each string consists of lowercase English letters.

For each k=1,2,,Nk=1,2,\ldots,N, solve the following problem.

Let T=SkT=S_k and consider performing the following two types of operations any number of times in any order:

  • Pay a cost of 11 to delete the last character of TT. This operation is possible when TT is not empty.
  • Pay a cost of 11 to add any lowercase English letter to the end of TT.

Find the minimum total cost needed to make TT either empty or match one of S1,S2,,Sk1S_1,S_2,\ldots,S_{k-1}.

Constraints

  • 1N2×1051\le N\le 2\times 10^5
  • Each SiS_i is a string of length at least 11 consisting of lowercase English letters.
  • i=1NSi2×105\displaystyle \sum_{i=1}^N |S_i|\le 2\times 10^5

Input

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

NN
S1S_1
S2S_2
\vdots
SNS_N

Output

Print NN lines. The ii-th line (1iN)(1\le i\le N) should contain the answer for k=ik=i.


Sample Input 1Copy

Copy
3
snuke
snuki
snuuk

Sample Output 1Copy

Copy
5
2
4

For k=1k=1, you can make TT empty by performing the delete operation five times.

For k=2k=2, you can make TT match S1S_1 by deleting the last character and then adding e to the end.

For k=3k=3, you can make TT match S2S_2 by deleting the last character twice, then adding k to the end, and finally adding i to the end.


Sample Input 2Copy

Copy
3
abc
arc
agc

Sample Output 2Copy

Copy
3
3
3

Sample Input 3Copy

Copy
8
at
atatat
attat
aatatatt
attattat
ttatta
tta
tt

Sample Output 3Copy

Copy
2
4
3
8
3
6
3
1


2025-04-28 (Mon)
04:09:39 +00:00