思维
题意:q个测试样例。n个字符串,找到一个字符串满足该字符串与所有的字符串最多只差一个字符。
思路:n,m范围很小,遍历n个字符串,找出他们的衍生字符串,即每位都变化26次。出现了n次的就是答案。
#include <iostream> #include <cmath> #include <cstdio> #include <cstring> #include <string> #include <map> #include <iomanip> #include <algorithm> #include <queue> #include <stack> #include <set> #include <vector> // #include <bits/stdc++.h> #define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define sp ' ' #define endl ' ' #define inf 0x3f3f3f3f; #define FOR(i,a,b) for( int i = a;i <= b;++i) #define bug cout<<"--------------"<<endl #define P pair<int, int> #define fi first #define se second #define pb(x) push_back(x) #define mp(a,b) make_pair(a,b) #define ms(v,x) memset(v,x,sizeof(v)) #define rep(i,a,b) for(int i=a;i<=b;i++) #define repd(i,a,b) for(int i=a;i>=b;i--) #define sca3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c)) #define sca2(a,b) scanf("%d %d",&(a),&(b)) #define sca(a) scanf("%d",&(a)); #define sca3ll(a,b,c) scanf("%lld %lld %lld",&(a),&(b),&(c)) #define sca2ll(a,b) scanf("%lld %lld",&(a),&(b)) #define scall(a) scanf("%lld",&(a)); using namespace std; typedef long long ll; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a/gcd(a,b)*b;} ll powmod(ll a, ll b, ll mod){ll sum = 1;while (b) {if (b & 1) {sum = (sum * a) % mod;b--;}b /= 2;a = a * a % mod;}return sum;} const double Pi = acos(-1.0); const double epsilon = Pi/180.0; const int maxn = 2e5+10; int main() { //freopen("input.txt", "r", stdin); int q; scanf("%d",&q); //set<pair<string,int> >st; while(q--) { int n,m; cin>>n>>m; map<string,int>mp; rep(i,1,n) { string s; cin>>s; set<string>st; rep(j,0,m-1) { char c = s[j]; rep(k,97,122) { s[j] = char(k); st.insert(s); } s[j] = c; } for(auto i : st){ mp[i]++; } } string ans; int flag = 0; for(auto i : mp){ int t = i.se; if(t == n){ flag = 1; ans = i.fi; } /* if(st.count(i) == n){ ans = i; flag = 1; break; }*/ } if(flag == 1){ cout<<ans<<endl; } else cout<<-1<<endl; } }