E - Keep Being Substring Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 700700

問題文

長さ NN の整数列 A=(A1,A2,,AN)A = (A_1, A_2, \ldots, A_N) が与えられます。 また、AA の長さ PP の連続な部分列 X=(X1,X2,,XP)X = (X_1, X_2, \ldots, X_P) と、AA の長さ QQ の連続な部分列 Y=(Y1,Y2,,YQ)Y = (Y_1, Y_2, \ldots, Y_Q) が与えられます。

XX に対して、下記の 44 つのいずれかを行うという操作を、好きな回数( 00 回でも良い)だけ行うことができます。

  • XX の先頭に任意の整数を 11 つ追加する。
  • XX の先頭の要素を削除する。
  • XX の末尾に任意の整数を 11 つ追加する。
  • XX の末尾の要素を削除する。

ただし、各操作の前後において、XXAA空でない連続な部分列でなければなりません。 XXYY と一致させるために行う操作回数の最小値を求めてください。 なお、本問題の制約下において、操作の繰り返しによって XXYY を必ず一致させられることが証明できます。

連続な部分列とは?

数列 X=(X1,X2,,XP)X = (X_1, X_2, \ldots, X_P) が数列 A=(A1,A2,,AN)A = (A_1, A_2, \ldots, A_N)連続な部分列であるとは、1lNP+11 \leq l \leq N-P+1 を満たす整数 ll が存在して、 すべての i=1,2,,Pi = 1, 2, \ldots, P について、Xi=Al+i1X_i = A_{l+i-1} が成り立つことです。

制約

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 1AiN1 \leq A_i \leq N
  • 1P,QN1 \leq P, Q \leq N
  • (X1,X2,,XP)(X_1, X_2, \ldots, X_P)(Y1,Y2,,YQ)(Y_1, Y_2, \ldots, Y_Q) は、(A1,A2,,AN)(A_1, A_2, \ldots, A_N) の連続な部分列
  • 入力はすべて整数

入力

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

NN
A1A_1 A2A_2 \ldots ANA_N
PP
X1X_1 X2X_2 \ldots XPX_P
QQ
Y1Y_1 Y2Y_2 \ldots YQY_Q

出力

答えを出力せよ。


入力例 1Copy

Copy
7
3 1 4 1 5 7 2
2
3 1
3
1 5 7

出力例 1Copy

Copy
3

下記の手順で操作すると、XXAA の空でない連続な部分列であるという条件を満たしたまま、XXYY に一致させることが出来ます。

  1. まず、XX の先頭の要素を削除する。その結果、X=(1)X = (1) となる。
  2. 次に、XX の末尾に 55 を追加する。その結果、X=(1,5)X = (1, 5) となる。
  3. さらに、XX の 末尾に 77 を追加する。その結果、X=(1,5,7)X = (1, 5, 7) となり、XXYY と一致する。

上記の手順の操作回数は 33 回であり、これが考えられる最小の操作回数です。


入力例 2Copy

Copy
20
2 5 1 2 7 7 4 5 3 7 7 4 5 5 5 4 6 5 6 1
6
1 2 7 7 4 5
7
7 4 5 5 5 4 6

出力例 2Copy

Copy
7

Score : 700700 points

Problem Statement

You are given an integer sequence A=(A1,A2,,AN)A = (A_1, A_2, \ldots, A_N) of length NN. Additionally, its contiguous subsequences of lengths PP and QQ are given: X=(X1,X2,,XP)X = (X_1, X_2, \ldots, X_P) and Y=(Y1,Y2,,YQ)Y = (Y_1, Y_2, \ldots, Y_Q).

You can perform the four operations on XX below any number of times (possibly zero) in any order.

  • Add an arbitrary integer at the beginning of XX.
  • Delete the element at the beginning of XX.
  • Add an arbitrary integer at the end of XX.
  • Delete the element at the end of XX.

Here, XX must be a non-empty contiguous subsequence of AA before and after each operation. Find the minimum total number of operations needed to make XX equal YY. Under the Constraints of this problem, it is guaranteed that one can always make XX equal YY by repeating operations.

What is a contiguous subsequence?

A sequence X=(X1,X2,,XP)X = (X_1, X_2, \ldots, X_P) is a contiguous subsequence of A=(A1,A2,,AN)A = (A_1, A_2, \ldots, A_N) when there is an integer ll satisfying 1lNP+11 \leq l \leq N-P+1 such that Xi=Al+i1X_i = A_{l+i-1} for every i=1,2,,Pi = 1, 2, \ldots, P.

Constraints

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 1AiN1 \leq A_i \leq N
  • 1P,QN1 \leq P, Q \leq N
  • (X1,X2,,XP)(X_1, X_2, \ldots, X_P) and (Y1,Y2,,YQ)(Y_1, Y_2, \ldots, Y_Q) are contiguous subsequences of (A1,A2,,AN)(A_1, A_2, \ldots, A_N).
  • All values in the input are integers.

Input

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

NN
A1A_1 A2A_2 \ldots ANA_N
PP
X1X_1 X2X_2 \ldots XPX_P
QQ
Y1Y_1 Y2Y_2 \ldots YQY_Q

Output

Print the answer.


Sample Input 1Copy

Copy
7
3 1 4 1 5 7 2
2
3 1
3
1 5 7

Sample Output 1Copy

Copy
3

You can make XX equal YY while keeping XX a non-empty contiguous subsequence of AA, as follows.

  1. First, delete the element at the beginning of XX. Now, you have X=(1)X = (1).
  2. Next, add 55 at the end of XX. Now, you have X=(1,5)X = (1, 5).
  3. Furthermore, add 77 at the end of XX. Now, you have X=(1,5,7)X = (1, 5, 7), which equal YY.

Here, you perform three operations, which is the fewest possible.


Sample Input 2Copy

Copy
20
2 5 1 2 7 7 4 5 3 7 7 4 5 5 5 4 6 5 6 1
6
1 2 7 7 4 5
7
7 4 5 5 5 4 6

Sample Output 2Copy

Copy
7


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