題目大意:
給定 n個長度為m的字串\(a_i\),能否找到一個字串s,使得s串與\(ai\)串至多存在一個不相同的字符,找出這樣的字串并輸出它,如果沒有找到則輸出-1,
思路:
暴力列舉求解,每次把 \(a_1\) 的所有可能是答案的字串給列舉一遍,然后判斷這個字串和 \(a_2 到 a_n\)的串是不是滿足條件,
代碼:
#include <bits/stdc++.h>
using namespace std;
const int N = 15;
int n, m;
string str[N];
bool check(string s, string str[]){
int cnt = 0;
for(int i = 1; i < n; i++){
cnt = 0;
for(int j = 0; j < m; j++){
if(s[j] != str[i][j]) cnt++;
}
if(cnt > 1) return false;
}
return true;
}
int main(){
int T;
cin >> T;
while(T--){
int flag = 0;
cin >> n >> m;
for(int i = 0; i < n; i++){
cin >> str[i];
}
string s = str[0];
for(int i = 0; i < m; i++){
for(char j = 'a'; j <= 'z'; j++){
char t = s[i];//每次保持一位變化,所以臨時保存該位置,以便列舉下一個位置時,上一個位置時原串中的字符
s[i] = j;
if(check(s, str)) {
flag = 1;
cout << s << endl;
break;
}
s[i] = t;//還原
}
if(flag) break;
}
if(flag == 0) cout << -1 << endl;
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/301647.html
標籤:其他
上一篇:HCNP Routing&Switching之路由控制、路由策略和IP-Prefix List
下一篇:淺談數位dp
