#P16883. [GKS 2022 #D] Touchbar Typing

    ID: 19211 远端评测题 3000ms 1024MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>动态规划 DP2022线性 DPGoogle Kick Start

[GKS 2022 #D] Touchbar Typing

Problem Description

Glide Typing task in Crowdsource app uses a new Google keyboard to type a word by sliding a finger across keys without lifting the finger, as shown in the animation below.

:::align{center} :::

To make the Glide Typing task more challenging, instead of a normal keyboard, we have a special linear keyboard KK that has all the keys in one row.

Imagine that you want to type a word SS that is NN characters long. The linear keyboard KK has MM keys. It is guaranteed that the keys cover all characters in SS. However, some of the keys may be duplicates. In other words, for each character in SS, there is one or more keys in KK mapped to the character. Note that, all characters and keys are represented as integers.

You may start with your finger on any key. It takes 11 second to move your finger from a key to an adjacent key. Due to Glide Typing, there is no pressing of a key. If the finger is currently at the key ii which has character KiK_i, and we want to type the character KjK_j at index jj, we will glide the finger from the key ii to the key jj, which takes ji|j - i| seconds. If your finger is at key xx, you can type character KxK_x any number of times instantly. You need to type string SS character by character. Formally, you need to type SiS_i before Si+1S_{i+1} for each 1iN11 \le i \le N - 1.

For example, suppose the word SS has characters: 1,2,2,3,41, 2, 2, 3, 4. You can start by keeping your finger on key with character 11 on the keyboard which is at index ii. Then you glide your finger to key which has character 22 which is at index jj. It would take ji|j - i| seconds. In order to type character 22 two times in string SS, you can do that in no additional time as jj=0|j - j| = 0 seconds. Then you can continue to glide your finger to type the other characters in the word SS sequentially.

Can you calculate the minimal time needed to type the word?

Input Format

The first line of the input gives the number of test cases, TT. TT test cases follow.

The first line of each test case contains one integer NN: the length of the word SS.

The second line of each test case contains NN integers: each SiS_i is the character at the ii-th index.

The third line of each test case contains one integer MM: the length of the keyboard KK.

The fourth line of each test case contains MM integers: each KiK_i is the character at the ii-th key.

Output Format

For each test case, output one line containing the minimal time needed to type the word. Case #xx: yy, where xx is the test case number starting from 11 and yy is the minimal time needed to type SS on the keyboard KK.

2
5
1 2 3 2 1
3
1 2 3
3
1 1 1
2
2 1
Case #1: 4
Case #2: 0
2
4
2 1 4 1
8
4 1 5 2 1 3 5 4
3
1 2 3
8
2 3 5 1 4 6 7 2
Case #1: 4
Case #2: 4

Hint

In Sample Case #11, we can take the following steps to type string SS in minimum time.

  • Start by keeping your finger on key K1K_1 which has character 11. We have now typed the first character of the string SS.
  • In order to type the second character 22 of the string SS, glide your finger to key K2K_2. It takes 21=1|2 - 1| = 1 additional second to glide the finger from index 11 to index 22.
  • In order to type the third character 33 of the string SS, glide your finger to key K3K_3. It takes 32=1|3 - 2| = 1 additional second to glide the finger from index 22 to index 33.
  • In order to type the fourth character 22 of the string SS, glide your finger to key K2K_2. It takes 23=1|2 - 3| = 1 additional second to glide the finger from index 33 to index 22.
  • In order to type the fifth character 11 of the string SS, glide your finger to key K1K_1. It takes 12=1|1 - 2| = 1 additional second to glide the finger from index 22 to index 11.
  • We have typed all characters of the string SS in a total of 44 seconds.

In Sample Case #22, we can take the following steps to type string SS in minimum time.

  • Start by keeping your finger on key K2K_2 which has character 11. We have now typed the first character of the string SS.
  • As our finger is on key K2K_2, we can type the character 11 any number of times, without any additional time. Hence, we can type the second and third characters of the string SS.
  • We have typed all characters of the string SS in a total of 00 seconds.

Limits

1T1001 \le T \le 100.

All characters in SS appears at least once in KK.

1Ki25001 \le K_i \le 2500.

1Si25001 \le S_i \le 2500.

Test Set 11

1N1001 \le N \le 100.

1M1001 \le M \le 100.

It is guaranteed that there are no duplicated keys in keyboard KK.

Test Set 22

1N1001 \le N \le 100.

1M1001 \le M \le 100.

Test Set 33

1N25001 \le N \le 2500.

1M25001 \le M \le 2500.