You are given nn strings a1,a2,…,an: all of them have the same length mm. The strings consist of lowercase English letters.
Find any string ss of length mm such that each of the given nn strings differs from s in at most one position. Formally, for each given string aiai, 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 tt test cases follow.
Each test case starts with a line containing two positive integers nn (1≤n≤10) and mm (1≤m≤10) — the number of strings and their length.
Then follow nn strings aiai, one per line. Each of them has length mm and consists of lowercase English letters.
Output
Print tt answers to the test cases. Each answer (if it exists) is a string of length mm 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
Copy
5 2 4 abac zbab 2 4 aaaa bbbb 3 3 baa aaa aab 2 2 ab bb 3 1 a b c
output
Copy
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.
思路:
1,数据范围太小了,不是dp就是暴力
2,只改变第一个字符串的一个位置,每个位置26个字母试一遍,复杂度O(26*n*m*m)
3,之所以这样,因为这个s只能和每一个字符串差一个字母,所以26个字母,每个位置去试,有一个统一简单的暴力规则很重要
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")//
const int maxj=3e5+100,mod=998244353;
int n,m;
string s[110];
bool ch(string str){
for(int i=1;i<=n;++i){
int cnt=0;
for(int j=0;j<m;++j){
if(str[j]!=s[i][j])cnt++;
}if(cnt>=2)return 0;
}return 1;
}
void solve(){//有技巧的枚举暴力
cin>>n>>m;
for(int i=1;i<=n;++i)cin>>s[i];
string str;
bool k=1;
for(int i=0;i<m;++i){
str=s[1];
for(char j='a';j<='z';++j){
str[i]=j;
if(ch(str)){
k=0;
break;
}
}if(!k){
break;
}
}
if(!k)cout<<str<<'\n';
else cout<<-1<<'\n';
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--)solve();
return 0;
}