题意:给N个字符串,要指定删除其中的M条,要求输出一条匹配字符串:将这M条字串的公有字符保留,其余换成'?',且该匹配字符不能误杀其他不能删除的字符串。若不存在这样的匹配字符串输出No。
# include <stdio.h>
# include <string.h>
char a[102][101];
int b[101], slen[101];
int main()
{
int n, m;
while(~scanf("%d%d",&n,&m))
{
memset(a[101], 0, sizeof(a[101]));
int len = 0, flag = 0, num=0, val=0;
for(int i=1; i<=n; ++i)
{
scanf("%s",a[i]);
slen[i] = strlen(a[i]);
}
for(int i=0; i<m; ++i)
{
scanf("%d",&b[i]);
if(flag)
continue;
if(!len)
len = slen[b[i]];
else
flag = (len != slen[b[i]]);//判断这M条字符串长度是否相等。
}
if(flag)
{
puts("No");
continue;
}
for(int i=0; i<len; ++i)
{
char c = a[b[0]][i];
int flag2 = 0;
for(int j=1; j<m; ++j)
if(a[b[j]][i] != c)
{
++num;
flag2 = 1;
break;
}
a[101][i] = flag2?'?':c;
}
val = len - num;//记录非'?'的字符个数。
num = 0;
for(int i=1; i<=n; ++i)//判断会不会误杀其他字符串。
if(slen[i] == len)
{
int tmp = 0;
for(int j=0; j<len; ++j)
if(a[101][j] != '?' && a[101][j] == a[i][j])
++tmp;
if(tmp == val)
++num;
}
if(num > m)
{
puts("No");
continue;
}
printf("Yes\n%s\n",a[101]);
}
return 0;
}