You are given nn strings a1,a2,…,ana1,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 ss in at most one position. Formally, for each given string aiai, there is no more than one position jj such that ai[j]≠s[j]ai[j]≠s[j].
Note that the desired string ss may be equal to one of the given strings aiai, 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 tt (1≤t≤1001≤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≤101≤n≤10) and mm (1≤m≤101≤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
5 2 4 abac zbab 2 4 aaaa bbbb 3 3 baa aaa aab 2 2 ab bb 3 1 a b cOutput
abab -1 aaa ab zNote
The first test case was explained in the statement.
In the second test case, the answer does not exist.
题意:多组输入,每组输入给出n个长度为m的字符串,试寻找是否存在一个字符串s,使得s与之前所有的字符串最多只有一位上的差异。如果存在,输出其中一个即可;如果不存在,输出-1。
题解:这题,刚拿到我真就又菜了,无从下手好吗,n个字符串,长度为m,我上哪去构造一个符合条件的字符串s?答案是无脑暴力枚举,笑了。方法就是通过修改第一个字符串的其中一位,构造成一个新的字符串s;然后判断后续的所有字符串与字符串s是否最多只有一位不同。真·无脑暴力枚举。苏菜菜暴风哭泣。
#include<iostream> #include<cstring> #include<algorithm> using namespace std; int t, n, m; char s[11][11]; //判断是否与剩余所有字符串都满足要求 int jud(char *str){ for(int i=1; i<n; i++){ int cnt=0;//统计不相同字符的个数 for(int j=0; j<m; j++) if(s[i][j] != str[j]) cnt++; if(cnt > 1) return 0;//超过一个字符不同即不满足要求 } return 1; } int main(){ scanf("%d", &t); while(t--){ scanf("%d%d", &n, &m); for(int i=0; i<n; i++) scanf("%s", s[i]); char str[11]; int flag = 0; for(int i=0; i<m; i++){ for(int j=0; j<26; j++){ strcpy(str, s[0]); str[i] = j + 'a';//复制第一个字符串之后修改其中一位 if(jud(str)){ flag = 1;break;} } if(flag) break; } if(flag) printf("%s\n", str); else printf("-1\n"); } return 0; }