当前位置:网站首页>C. Most Similar Words
C. Most Similar Words
2022-06-29 21:35:00 【Felven】
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.
Problem solving instructions : Water problem , Directly traverse to find the minimum value .
#include"stdio.h"
#include"math.h"
int main()
{
int k, n, m, i, j, x;
int min, sum;
char C[51][9];
scanf("%d", &k);
while (k--) {
scanf("%d %d", &n, &m);
min = 209;
for (i = 0; i<n; i++)
{
scanf("%s", C[i]);
}
for (i = 0; i<n; i++)
{
for (j = i + 1; j<n; j++)
{
sum = 0;
for (x = 0; x<m; x++)
{
if (C[i][x]>C[j][x])
{
sum = sum + (C[i][x] - C[j][x]);
}
else
{
sum = sum + (C[j][x] - C[i][x]);
}
}
if (sum<min)
{
min = sum;
}
}
}
printf("%d\n", min);
}
return 0;
}边栏推荐
- Rsync 的简单应用与配置
- leetcode:370. Interval addition
- 高校如何基于云原生构建面向未来的智慧校园?全栈云原生VS传统技术架构
- [cloud native practice] kubesphere practice - Multi tenant system practice
- Verilog realizes serial communication and sends it to the nixie tube
- 阿里巴巴关键字搜索商品API接口(item_search-按关键字搜索商品接口),阿里巴巴搜索API接口
- 亚马逊商品详情API接口-(item_get-获得AMAZON商品详情接口),亚马逊详情API接口
- 推荐书籍--白夜行
- CORDIC based Signal Processor desgn
- 透过华为军团看科技之变(五):智慧园区
猜你喜欢

时钟树综合(CTS)

Vipshop product details API interface (item_get- get vipshop product details interface), vipshop details API interface

Summary of document level symbols under different systems
![[advanced ROS chapter] Lecture 4: duplicate names in ROS (nodes, topics and parameters)](/img/25/85e8c55605f5cc999a8e85f0a05f93.jpg)
[advanced ROS chapter] Lecture 4: duplicate names in ROS (nodes, topics and parameters)

高校如何基于云原生构建面向未来的智慧校园?全栈云原生VS传统技术架构

VHDL电话计费系统设计

Win10 add SSH public key

LSF bsub command

Visual analysis and display effect of summer data

Star ring technology data security management platform defender heavy release
随机推荐
What is a SYN Flood attack? How to protect?
What are the mainstream brands of smart door locks? What characteristics should we pay attention to when purchasing door locks?
我的创作纪念日
Recommended books -- walking in the daytime and at night
CORDIC based Signal Processor desgn
How can colleges and universities build future oriented smart campus based on cloud native? Full stack cloud native vs traditional technology architecture
Matlab adds noise / disturbance to data
路由汇总带来的三层环路-解决实验
leetcode:724. 寻找数组的中心下标
Application of VoIP push in overseas audio and video services
Navigation exercises [microcomputer principles] [exercises]
如何在 SAP BTP ABAP 编程环境里直接调用 ABAP On-Premises 系统的 RFC 函数
About Effect Size
Navigation experiment [microcomputer principle] [experiment]
GoAhead 翻译—Active Server Pages
Alibaba product details API interface (item_get- get product details interface), Alibaba API interface
LeetCode 1. Sum of two numbers
Basic qualities of management personnel
Selection of materials for space conductive disc slip ring
VHDL电话计费系统设计