Word Swap
Time limit1sMemory limit256 MB
Sum the per-position letter differences between each pair of equal-length words and report the coins earned, paid, or free.
- Level
Easy2 of 10
- Topics
- Implementation, String
- Solved
- No attempts yet
Problem
The carnival is running a new game next year. The carnival worker picks a word, writes it on a piece of paper, and tells you only its length. Call the written word Word 1, and call the word of the same length that you guess Word 2. You earn or pay coins according to how different the two words are, that is, how much work it takes to swap the letters of Word 1 into the letters of Word 2. Agnes just turned 6, has barely started reading, and cannot do arithmetic yet, so she wants help playing this game.
The carnival set these coin rules.
- The game uses only the letters a to z.
- The difference between the words is decided position by position.
- At each position: (1) if the two letters are the same, no coin is paid or earned. (2) if the letter of Word 1 comes before the letter of Word 2 in the alphabet, you pay 1 coin for every letter that sits between the two letters, plus 1 more coin. (3) if the letter of Word 1 comes after the letter of Word 2, you earn that same amount.
For example, if the carnival worker writes agnes and you guess heard, the calculation is the table below.
Add the coins earned at each position and subtract the coins paid to get the total for the pair. A positive total is what you earn, a negative total is what you pay, and a total of 0 moves no coins.
You might call this a dull game, since guessing a word like aal or abut of the right length always earns coins. The real carnival game adds a twist: earn too much and you get nothing. This problem ignores that twist.
Write a program that reads several pairs of words and reports the coins earned or paid for each pair.
Input
The first line contains the number of test cases . ()
Each of the next lines contains two words separated by one space. The first word is Word 1 and the second word is Word 2. The two words have the same length, consist of lowercase letters only, and have length between 1 and 100.
Output
Print one line per test case. Let A be Word 1, B be Word 2, and be the total.
- If , print
Swapping letters to make A look like B earned S coins. - If , print
Swapping letters to make A look like B cost S coins., whereSis replaced by the absolute value of the total. - If , print
Swapping letters to make A look like B was FREE.
Write coins even when the amount is 1.