Spy-string CodeForces - 1360F(暴力)

You are given n strings a1,a2,…,an: all of them have the same length m. The strings consist of lowercase English letters.

Find any string s of length m such that each of the given n strings differs from s in at most one position. Formally, for each given string ai, there is no more than one position j such that ai[j]≠s[j].

Note that the desired string s may be equal to one of the given strings ai, or it may differ from all the given strings.

For example, if you have the strings abac and zbab, then the answer to the problem might be the string abab, which differs from the first only by the last character, and from the second only by the first.

Input
The first line contains an integer t (1≤t≤100) — the number of test cases. Then t test cases follow.

Each test case starts with a line containing two positive integers n (1≤n≤10) and m (1≤m≤10) — the number of strings and their length.

Then follow n strings ai, one per line. Each of them has length m and consists of lowercase English letters.

Output
Print t answers to the test cases. Each answer (if it exists) is a string of length m consisting of lowercase English letters. If there are several answers, print any of them. If the answer does not exist, print “-1” (“minus one”, without quotes).

Example
Input
5
2 4
abac
zbab
2 4
aaaa
bbbb
3 3
baa
aaa
aab
2 2
ab
bb
3 1
a
b
c
Output
abab
-1
aaa
ab
z
Note
The first test case was explained in the statement.

In the second test case, the answer does not exist.
思路:数据量很水,可以直接暴力。对第一个字符串的每一个位置,我们尝试用26个字母去替换,如果找到一个符合的,就直接退出就ok了。否则就是不可以。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

const int maxx=11;
string s[maxx];
int n,m;
inline int check(string ss)
{
	for(int i=2;i<=n;i++)
	{
		int cnt=0;
		for(int j=0;j<m;j++) if(s[i][j]!=ss[j]) cnt++;
		if(cnt>1) return 0;
	}
	return 1;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++) cin>>s[i];
		string ss;
		int flag=0;
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<26;j++)
			{
				ss=s[1];
				ss[i]=(char)('a'+j); 
				if(check(ss)) 
				{
					flag=1;
					break;
				}
			}
			if(flag) break;
		}
		if(flag) cout<<ss<<endl;
		else cout<<"-1"<<endl;
	}
	return 0;
}

努力加油a啊,(o)/~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值