[Solution] Minimum Absolute Score CodeChef Solution
Problem
You are given two strings and of length consisting of lowercase English letters. Your objective is to make both the strings equal.
You can apply one of the following operations at each index :
- Convert char to by doing right cyclic shift of character . This increases your score by amount equal to cyclic shifts done.
- Convert char to by doing right cyclic shift of character . This decreases your score by amount equal to cyclic shifts done.
Your starting score is zero.
If the operations are applied optimally, find the minimum absolute score possible after making both the strings equal.
Note: A single right cyclic shift converts one character to the next in alphabetical order, except for which goes to . That is, the sequence looks like
So, for example converting to requires right cyclic shifts, and converting to requires .
Input Format
- The first line of input will contain a single integer , denoting the number of test cases.
- Each test case consists of three lines of input.
- The first line of each test case contains one integer — the length of strings and .
- The second line contains string .
- The third line contains string .
Output Format
For each test case, output on a new line the minimum absolute score possible after making both the strings equal.
Explanation:
Test case : The minimum absolute score can be obtained as follows:
- Apply operation at position , converting to for a cost of .
- Apply operation at position , converting to for a cost of .
- Apply operation at position , converting to for a cost of .
The score is then , with absolute value . This is the lowest possible absolute value attainable.
Test case : Apply operations as follows:
- Operation at index , for a cost of
- Operation at index , for a cost of
- Operation at index , for a cost of
This gives us a final score of , which has absolute value . It is not possible to do better than this.
No comments:
Post a Comment