题意:给你n个字符串,你需要找出一个pattern串,这个字符串如果跟n个字符串中的一个匹配,就要删除该字符串,两个字符串匹配是指对应字符相等或者pattern串中该位置为“?”,题目要求只删除给定字符串而不删除其他字符串,如果找不到这样的一个pattern串就输出no,反之输出yes和该字符串。
解题思路:直接模拟即可,先找出它要求删除的字符串,比较他们的字符串长度,如果不相等直接输出no,因为不可能找到这样一个字符串了,如果所有的字符串长度相等,那么接下来先找出字符串,从每个字符串的第一位开始,如果所有要删除的字符串的这一位都相等,那么pattern串的这一位直接置为这一个字符,反之,置为“?”,找完pattern串之后,还要用找出的pattern串与其他不需要删除的字符串比较,只有pattern串与它们都不匹配才输出yes,并且输出pattern串,反之输出no。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1010;
string s[maxn];
int n,m;
int a[maxn];
bool d[maxn];
int len;
char pattern[maxn];
bool ok(string x)
{
for(int i = 0; i < len; i++)
{
if(pattern[i] != '?'&&pattern[i] != x[i])
return false;
}
return true;
}
int main()
{
scanf("%d%d",&n,&m);
bool ans = true;
memset(d,false,sizeof(d));
for(int i = 1; i <= n; i++)
{
cin>>s[i];
}
for(int i = 1; i <= m; i++)
{
scanf("%d",&a[i]);
d[a[i]] = true;
}
len = s[a[1]].size();
for(int i = 2; i <= m; i++)
{
if(s[a[i]].size() != len)
{
ans = false;
break;
}
}
if(ans)
{
bool flag;
bool res = false;
for(int i = 0; i < len; i++)
{
flag = false;
char term = s[a[1]][i];
for(int j = 2; j <= m; j++)
{
if(s[a[j]][i] != term)
{
pattern[i] = '?';
flag = true;
break;
}
}
if(!flag) pattern[i] = term;
}
for(int i = 1; i <= n; i++)
{
if(s[i].size()==len&&!d[i])
{
if(ok(s[i]))
{
res = true;
break;
}
}
}
if(res) printf("No\n");
else
{
printf("Yes\n");
for(int i = 0; i < len; i++)
{
printf("%c",pattern[i]);
}
printf("\n");
}
}
else
{
printf("No\n");
}
return 0;
}