当前位置:网站首页>C. Most Similar Words
C. Most Similar Words
2022-06-29 15:36: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.
解题说明:水题,直接遍历找出最小值即可。
#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;
}边栏推荐
- BFD principle and configuration
- Introduction to radar antenna
- C language big job - Matching System
- I want to know where I can open an account in Nanning? In addition, is it safe to open a mobile account?
- 商业智能BI与业务管理决策思维之三:业务质量分析
- PWM to 0-5v/0-10v/1-5v linear signal transmitter
- 墨天轮“高可用架构”干货文档分享(含Oracle、MySQL、PG资料124篇)
- C#学习二:堆和栈
- 从第三次技术革命看企业应用三大开发趋势
- Business Intelligence BI and business management decision-making thinking No. 3: business quality analysis
猜你喜欢

About sql+nosql: newsql database

【云原生】Nacos-TaskManager 任务管理的使用

GWD:基于高斯Wasserstein距离的旋转目标检测 | ICML 2021

File常用工具类, 流相关运用 (记录)

Three development trends of enterprise application viewed from the third technological revolution

When easygbs calls the interface for obtaining real-time snapshots, how to solve the problem of white squares?

File常用工具類, 流相關運用 (記錄)

攻防演练之战前扫雷:漏洞管理的5大措施

智能聊天机器人的优势在哪里?资深独立站卖家告诉你!

防范企业数据泄露,就用网络安全产品堡垒机!
随机推荐
Symfony framework security component firewall configuration
李飞飞划重点的「具身智能」,走到哪一步了?
2022 OpenVINO DevCon 大揭秘!英特尔携众多合作伙伴深化开发者生态建设,释放AI产业创新潜能
Mysql database naming conventions PDF
Dynamically listening for DOM element height changes
Complex convolutional neural network: cv-cnn
京东健康回应拟以 3.554 亿美元收购京东资产:与宠物健康产品品类相关
SSL v** technical principle
Basic composition of radar
发明了杀毒软件之后,他选择做一个极品混混
LeetCode-470-用Rand7()实现Rand10()
What is the time complexity of the redis command?? (the actual question is about the underlying structure of redis)
C#学习二:堆和栈
"Game engine shallow in shallow out" 98 Substancepainer plug-in development
radar transmitter
2022-06-29日报: 李飞飞划重点的「具身智能」,走到哪一步了?
It is expected to significantly improve the computational performance of integrated photonic circuits. The Tsinghua team proposed a diffraction pattern neural network framework
cmake学习-2
C语言大作业——匹配系统
GWD:基于高斯Wasserstein距离的旋转目标检测 | ICML 2021