-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1360f.cpp
More file actions
52 lines (49 loc) · 954 Bytes
/
Copy path1360f.cpp
File metadata and controls
52 lines (49 loc) · 954 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* author: vishnus
* created: 2022-08-29
**/
#include <bits/stdc++.h>
using namespace std;
// Idea: Try all strings that differ from the first string by one character.
int main() {
#ifdef ONLINE_JUDGE
ios::sync_with_stdio(false);
cin.tie(0);
#endif
int tt;
cin >> tt;
while (tt--) {
int n, m;
cin >> n >> m;
vector<string> s(n);
for (auto &e : s) {
cin >> e;
}
string ans;
auto Check = [&](string cur) {
for (int i = 0; i < n; i++) {
int dif = 0;
for (int j = 0; j < m; j++) {
dif += s[i][j] != cur[j];
}
if (dif > 1) {
return;
}
}
ans = cur;
};
Check(s[0]);
for (int i = 0; i < m; i++) {
for (char c = 'a'; c <= 'z'; c++) {
string ne = s[0];
ne[i] = c;
Check(ne);
}
}
if (ans.empty()) {
cout << -1 << '\n';
continue;
}
cout << ans << '\n';
}
}