Transform the String
Memory limit1024 MB
For each test case, find the minimum number of cyclic alphabet steps needed to turn every letter of S into some letter present in F.
- Level
Easy3 of 10
- Topics
- String, Brute force, Implementation, Greedy
- Solved
- No attempts yet
Problem
You are given a string that denotes a padlock consisting of lowercase English letters. You are also given a string consisting of a set of favorite lowercase English letters. You may 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 alphabetical order. For example, for the letter c, you may change it to either b or d in an operation. The letters are considered in cyclic order, so the preceding letter for a is z, and the following letter for z is a.
Find the minimum number of operations required so that every letter in string after the operations is present in string .
Input
The first line of the input gives the number of test cases, . test cases follow.
Each test case consists of two lines.
The first line of each test case contains the string .
The second line of each test case contains the string .
Output
For each test case, output one line containing Case #x: y, where is the test case number (starting from 1) and is the minimum number of operations required so that every letter in string after the operations is one of the characters in string .
Constraints
- .
- the length of .
- consists only of lowercase English letters.
- consists only of distinct lowercase English letters.
- The letters in string are sorted lexicographically.