CF1676C.Most Similar Words

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given nn words of equal length mm , consisting of lowercase Latin alphabet letters. The ii -th word is denoted sis_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=111 + 10 + 0 + 0 = 11 .

Find the minimum difference of sis_i and sjs_j such that (i<j)(i < j) . In other words, find the minimum difference over all possible pairs of the nn words.

输入格式

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

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

Then follows nn lines, the ii -th of which containing a single string sis_i of length mm , 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

说明/提示

For the second test case, one can show that the best pair is ("abb","bef"), which has difference equal to 88 , 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 33 moves and change the third character of the second string to 'b' in 44 moves, thus making in total 1+3+4=81 + 3 + 4 = 8 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 3535 .

For the fourth test case, there is a pair of strings which is already equal, so the answer is 00 .

首页