F. Spy-string
题目链接-F. Spy-string
题目大意
给定
n
n
n个长度相同的字符串,如果能构造出一个字符串,使得其于每个字符串不同的字母个数不大于
1
1
1,则输出这个字符串,否则输出
−
1
-1
−1
解题思路
暴
力
暴力
暴力
- 枚举每个字符串
m
m
m个位置的字母,每个位置都枚举一下
26
26
26个字母,用
map
记录原字符串和改变一个字母后的所有字符串出现的次数 - 最后遍历
map
容器,判断是否有一个字符串的出现次数大于等于 n n n,若有则输出这个字符串,若没有则输出-1
- 具体操作见代码
附上代码
#pragma GCC optimize("-Ofast","-funroll-all-loops")
#include<bits/stdc++.h>
#define int long long
#define lowbit(x) (x &(-x))
#define endl '\n'
using namespace std;
const int INF=0x3f3f3f3f;
const int dir[4][2]={-1,0,1,0,0,-1,0,1};
const double PI=acos(-1.0);
const double e=exp(1.0);
const double eps=1e-10;
const int M=1e9+7;
const int N=2e5+10;
typedef long long ll;
typedef pair<int,int> PII;
typedef unsigned long long ull;
string s[15];
map<string,int> mp;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int t;
cin>>t;
while(t--){
int n,m;
cin>>n>>m;
mp.clear();
for(int i=0;i<n;i++)
cin>>s[i];
for(int i=0;i<n;i++){
mp[s[i]]++;//记录原字符串出现的次数
for(int j=0;j<m;j++){
for(char c='a';c<='z';c++){
if(c!=s[i][j]){//若该位字母与将替换它的字母不一样
char tmp=s[i][j];
s[i][j]=c;//用c替换该位字母
mp[s[i]]++;//记录新字符串出现的次数
s[i][j]=tmp;//还原字符串
}
}
}
}
bool ass=0;
for(auto it:mp){
if(it.second>=n){
cout<<it.first<<endl;
ass=1;
break;
}
}
if(!ass) cout<<-1<<endl;
}
return 0;
}