欢迎来到起遇信息学
起遇信息学正处于上线筹建阶段,以下功能已全部开放免费体验: ✅ 完整题库浏览与代码提交评测(C / C++ / Python / Java 等) ✅ 入门到进阶的系列课程试读、作业与考试 ✅ AI 提示、AI 作业分析等智能助教功能 ✅ 赛事模拟与个人能力报告 ✅ 邮箱注册开放 ⏳ 付费课程订阅与微信/支付宝支付通道 ⏳ 手机号登录,微信扫码登录、微信公众号绑定 使用中如遇任何问题,欢迎通过页面底部 **"联系我们"** 与我们沟通。
CF1676C.Most Similar Words
Most Similar Words
You are given words of equal length , consisting of lowercase Latin alphabet letters. The -th word is denoted .
In one move you can choose any position in any single word and change the letter at that position to the previous or next letter in alphabetical order. For example:
- you can change '
e' to 'd' or to 'f'; - '
a' can only be changed to 'b'; - '
z' can only be changed to 'y'.
The difference between two words is the minimum number of moves required to make them equal. For example, the difference between "best" and "cost" is .
Find the minimum difference of and such that . In other words, find the minimum difference over all possible pairs of the words.
Input
The first line of the input contains a single integer () — the number of test cases. The description of test cases follows.
The first line of each test case contains integers and (, ) — the number of strings and their length respectively.
Then follows lines, the -th of which containing a single string of length , consisting of lowercase Latin letters.
Output
For each test case, print a single integer — the minimum difference over all possible pairs of the given strings.
Note
For the second test case, one can show that the best pair is ("abb","bef"), which has difference equal to , which can be obtained in the following way: change the first character of the first string to 'b' in one move, change the second character of the second string to 'b' in moves and change the third character of the second string to 'b' in moves, thus making in total moves.
For the third test case, there is only one possible pair and it can be shown that the minimum amount of moves necessary to make the strings equal is .
For the fourth test case, there is a pair of strings which is already equal, so the answer is .
Samples
6
2 4
best
cost
6 3
abb
zba
bef
cdu
ooo
zzz
2 7
aaabbbc
bbaezfe
3 2
ab
ab
ab
2 8
aaaaaaaa
zzzzzzzz
3 1
a
u
y
11
8
35
0
200
4
在线编程 IDE
建议全屏模式获得最佳体验
| 进入全屏编程 | Alt+E |
| 递交评测 | Ctrl+Enter |
| 注释/取消注释 | Ctrl+/ |
| 缩放字体 | Ctrl+滚轮 |