题库练习 Most Similar Words
← 上一题 下一题 →

A14970 | Most Similar Words

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

You are given $n$ words of equal length $m$ , consisting of lowercase Latin alphabet letters. The $i$ -th word is denoted $s_i$ .

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 $1 + 10 + 0 + 0 = 11$ .

Find the minimum difference of $s_i$ and $s_j$ such that $(i < j)$ . In other words, find the minimum difference over all possible pairs of the $n$ words.

输入格式

The first line of the input contains a single integer $t$ ( $1 \le t \le 100$ ) — the number of test cases. The description of test cases follows.

The first line of each test case contains $2$ integers $n$ and $m$ ( $2 \leq n \leq 50$ , $1 \leq m \leq 8$ ) — the number of strings and their length respectively.

Then follows $n$ lines, the $i$ -th of which containing a single string $s_i$ of length $m$ , consisting of lowercase Latin letters.

输出格式

For each test case, print a single integer — the minimum difference over all possible pairs of the given strings.

输入输出样例

输入 #1
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
输出 #1
11
8
35
0
200
4
C++ 编辑器
输入
输出