[Solution] Touchbar Typing Round D 2022 | Kick Start 2022 Solution
Problem
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.
To make the Glide Typing task more challenging, instead of a normal keyboard, we have a special linear keyboard that has all the keys in one row.
Imagine that you want to type a word that is characters long. The linear keyboard has keys. It is guaranteed that the keys cover all characters in . However, some of the keys may be duplicates. In other words, for each character in , there is one or more keys in 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 second to move your finger from a key to an adjacent key. Due to Glide Typing, there is no pressing a key. If the finger is currently at the key which has character , and we want to type the character at index , we will glide the finger from the key to the key , which takes seconds. If your finger is at key , you can type character any number of times instantly. You need to type string character by character. Formally, you need to type before for each .
For example, suppose the word has characters: 1
, 2
, 2
, 3
, 4
. You can start by keeping your finger on key with character 1
on the keyboard which is at index . Then you glide your finger to key which has character 2
which is at index . It would take seconds. In order to type character 2
two times in string , you can do that in no additional time as seconds. Then you can continue to glide your finger to type the other characters in the word sequentially.
Can you calculate the minimal time needed to type the word?
Input
The first line of the input gives the number of test cases, . test cases follow.
The first line of each test case contains one integer : the length of the word .
The second line of each test case contains integers: each is the character at the -th index.
The third line of each test case contains one integer : the length of the keyboard .
The fourth line of each test case contains integers: each is the character at the -th key.
Output
For each test case, output one line containing the minimal time needed to type the word. Case #:
, where is the test case number (starting from 1) and is the minimal time needed to type on the keyboard .
Limits
Memory limit: 1 GB.
.
All characters in appears at least once in .
.
.
Test Set 1
Time limit: 20 seconds.
.
.
It is guaranteed that there are no duplicated keys in keyboard .
Test Set 2
Time limit: 20 seconds.
.
.
Test Set 3
Time limit: 40 seconds.
.
.
No comments:
Post a Comment