Codeforces Round #644 (Div. 3) F.Spy-string

Codeforces Round #644 (Div. 3) F.Spy-string

题目链接

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

字符串水题~
我们观察一下数据直接确定用暴力
因为答案和每个字符串最多相差一个字符,那么我们反过来想,如果把每个字符串的每个字符都用 26 个字母替换一遍,判断是否符合题意即可,AC代码如下:

#include
using namespace std;
typedef long long ll;
int n,m,t;
string s[15];
int judge(string S){
     
    for(int i=0;i<n;i++){
     
        int num=0;
        for(int j=0;j<m;j++)
            if(S[j]!=s[i][j]) num++;
        if(num>1) return 0;
    }
    return 1;
}
main(){
     
    cin>>t;
    while(t--){
     
        int flag=0;
        string ans;
        cin>>n>>m;
        for(int i=0;i<n;i++) cin>>s[i];
        for(int i=0;i<n;i++){
     
            for(int j=0;j<m;j++){
     
                string S=s[i];
                for(char c='a';c<='z';c++){
     
                    S[j]=c;
                    if(judge(S)){
     
                        flag=1;
                        ans=S;
                    }
                }
            }
        }
        if(flag) cout<<ans<<endl;
        else puts("-1");
    }
}

你可能感兴趣的:(字符串,思维,Codeforces)