#P16865. [GKS 2021 #H] Transform the String

[GKS 2021 #H] Transform the String

Problem Description

You are given a string SS which denotes a padlock consisting of lower case English letters. You are also given a string FF consisting of set of favorite lower case English letters. You are allowed to perform several operations on the padlock. In each operation, you can change one letter of the string to the one following it or preceding it in the alphabetical order. For example: for the letter cc, you are allowed to change it to either bb or dd in an operation. The letters can be considered in a cyclic order, i.e., the preceding letter for letter aa would be letter zz. Similarly, the following letter for letter zz would be letter aa.

Your aim is to find the minimum number of operations that are required such that each letter in string SS after applying the operations, is present in string FF.

Input Format

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

Each test case consists of two lines.

The first line of each test case contains the string SS.

The second line of each test case contains the string FF.

Output Format

For each test case, output one line containing Case #xx: yy, where xx is the test case number (starting from 11) and yy is the minimum number of operations that are required such that each letter in string SS after applying the operations, is one of the characters in string FF.

2
abcd
a
pppp
p
Case #1: 6
Case #2: 0
3
pqrst
ou
abd
abd
aaaaaaaaaaaaaaab
aceg
Case #1: 9
Case #2: 0
Case #3: 1

Hint

In Sample Case #11, all the letters in string SS should be converted to letter aa. We can keep on changing the letters to its preceding letter till we reach the letter aa. We do not need to change the first letter as it is already aa. The second letter needs 11 operation to change it to aa. The third letter needs 22 operations to change it to aa. The fourth letter needs 33 operation to change it to aa. Hence, we need a total of 66 operations to change string SS such that all letters are changed to aa.

In Sample Case #22, string SS already contains only the favorite letter from string FF. Hence, we do not require any more operations.

Limits

1T1001 \le T \le 100.

1S1051 \le |S| \le 10^5.

SS only consists of lower case English letters.

FF only consists of distinct lower case English letters.

The letters in string FF are lexicographically sorted.

Test Set 11

The length of FF is 11.

Test Set 22

1F261 \le |F| \le 26.