John is given a string S, and another string F with DISTNICT letters. He is asked to change letters (according to the rules below) in S in order that S only consists letters from F.
For every operation, he can change one letter in S into the one following it or preceding it in alphabetical order. For example, he can change "m" to either "n" or "l". Assume the letters are in a cylic order, meaning he can change the letter "a" to either "b" or "z".
Your job is to find out the the number of minimum operations N John needs to do.
(Google Kick Start 2021 - Round H - Transform the String)
The first line of input gives the number of test cases T with T cases following.
With every case, the first line contains the string S. Following by a line containing the string F with distinct letters.
All string inputs only consist of lowercase letters.
For every test case, output only one line containing N.
2 aceg c pppp p
8 0
3 pqrstuv ow abd abd aaaaaaaaaaaab aceg
16 0 1
Sample Input#1:
For the first test case, "a" -> "b" -> "c" (2 operations), "c" (0 operation), "e" -> "d" -> "c" (2 operations), "g" -> "f" -> "e" -> "d" -> "c" (4 operations). Total N = 2 + 2 + 4 = 8
Sample Input#2:
For the first test case, "p" -> "o" (1 op.), "q" -> "p" -> "o" (2 op.), "r" -> "q" -> "p" -> "o" (3 op.), "s" -> "r" -> "q" -> "p" -> "o" OR "s" -> "t" -> "u" -> "v" -> "w" (both 4 op.), and so on (3 op. for "t", 2 op. for "u" and 1 op. for "v"). Total N = 1 + 2 + 3 + 4 + 3 + 2 + 1 = 16
ID | User | Problem | Subject | Hit | Post Date |
沒有發現任何「解題報告」 |