#P483. String Operation

    ID: 483 Type: Default 10000ms 48MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>競賽題目2021 Google Kick Start Round H

String Operation

題目描述

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.

Samples

["2\r\naceg\r\nc\r\npppp\r\np","3\r\npqrstuv\r\now\r\nabd\r\nabd\r\naaaaaaaaaaaab\r\naceg"]
["8\r\n0","16\r\n0\r\n1"]

提示

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

原始資料

  • Zero1 題號:a483
  • Hydro 題號:Z0483
  • Locale:en_US
  • Display:open