Codeforces Round #790 (Div. 4)C. Most Similar Words

21 篇文章 0 订阅
10 篇文章 0 订阅

        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

 

#include <iostream>
#include <algorithm>
#include <cstring>
 
using namespace std;
 
const int N=1010;
 
int n,m;
int f[N];
int t;
int g[N];
 
int main()
{
	int t;
	cin>>t;
	string s[N];
	while(t--)
	{
		cin>>n>>m;
		for(int i=0;i<n;i++)
		{
			cin>>s[i];
		}
		int sum=10000000;
		for(int i=0;i<n;i++)
		{
			for(int j=i+1;j<n;j++)
			{
				int ans=0;
				for(int k=0;k<m;k++)
				{ 
					ans+=abs(s[j][k]-s[i][k]);
				}
				sum=min(sum,ans);
			
			}
		}
		cout<<sum<<endl;
	}
	return 0;
 } 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值