• C. Most Similar Words


    time limit per test

    2 seconds

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    You are given nn words of equal length mm, consisting of lowercase Latin alphabet letters. The ii-th word is denoted sisi.

    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 sisi and sjsj such that (i<j)(i<j). In other words, find the minimum difference over all possible pairs of the nn words.

    Input

    The first line of the input contains a single integer tt (1≤t≤1001≤t≤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 (2≤n≤502≤n≤50, 1≤m≤81≤m≤8) — the number of strings and their length respectively.

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

    Example

    input

    Copy

    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
    

    output

    Copy

    11
    8
    35
    0
    200
    4
    

    Note

    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.

    解题说明:水题,直接遍历找出最小值即可。

    1. #include"stdio.h"
    2. #include"math.h"
    3. int main()
    4. {
    5. int k, n, m, i, j, x;
    6. int min, sum;
    7. char C[51][9];
    8. scanf("%d", &k);
    9. while (k--) {
    10. scanf("%d %d", &n, &m);
    11. min = 209;
    12. for (i = 0; i<n; i++)
    13. {
    14. scanf("%s", C[i]);
    15. }
    16. for (i = 0; i<n; i++)
    17. {
    18. for (j = i + 1; j<n; j++)
    19. {
    20. sum = 0;
    21. for (x = 0; x<m; x++)
    22. {
    23. if (C[i][x]>C[j][x])
    24. {
    25. sum = sum + (C[i][x] - C[j][x]);
    26. }
    27. else
    28. {
    29. sum = sum + (C[j][x] - C[i][x]);
    30. }
    31. }
    32. if (sum<min)
    33. {
    34. min = sum;
    35. }
    36. }
    37. }
    38. printf("%d\n", min);
    39. }
    40. return 0;
    41. }

  • 相关阅读:
    C++中将 sizeof() 用于类
    喜马拉雅 Redis 与 Pika 缓存使用军规
    实力认证!Coremail连续9次入围安全牛《中国网络安全行业全景图》
    【Python百日进阶-WEB开发】Day172 - Django案例:04用户模型类
    【AI视野·今日CV 计算机视觉论文速览 第260期】Wed, 4 Oct 2023
    嵌入式基础_STM32F103C8T6从零开始建立一个项目(库函数)
    基于FTP协议的文件上传与下载
    leetcode-每日一题-119-杨辉三角2(简单,dp)
    学习偏态分布的相关知识和原理的4篇论文推荐
    Python交叉验证实现
  • 原文地址:https://blog.csdn.net/jj12345jj198999/article/details/125453245