Date:2022.01.29
题意:给定n个字符串,求解一个串,使所得串与n个串中的每一个至多含有一个不同的字符。不存在输出-1。
思路:开始硬枚举半天,首先发现任意两个串间存在>2个不同字符一定不满足,其次枚举改变不同的字符,在与其它匹配,大可不必。直接枚举每个串在每个位置是其它字母时的衍生串,map记录字符串被衍生的次数,每一个都<=n(因为最多被n个串中的每一个衍生1次),找到被每个串衍生1次即总共被衍生n次的串即可。此外注意每个串原串也算如map,但只算入一次。
代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> PII;
LL n,m,k,t;
string p[N];
set<char>s[N];
char c[N];
int main()
{
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
LL bxt=0;cin>>t;
while(t--)
{
map<string,LL>q;
cin>>n>>m;
for(int i=0;i<n;i++)
{
cin>>p[i];string ss=p[i];q[ss]++;
for(int j=0;j<m;j++)
{
for(char c='a';c<='z';c++)
{
string sss=ss;
if(sss[j]==c) continue;
sss[j]=c;q[sss]++;
}
}
}
bool flag=false;
for(auto it:q)
if(it.second==n) {flag=true;cout<<it.first<<endl;break;}
if(!flag) cout<<"-1"<<endl;
}
return 0;
}