本题是暴力法比较的题目了。
主要是检查每一位是否可以不使用?,如果可以优先使用其他任何字母,如果不可以就必须使用?。
找到一个可以和所有给出的字符串都匹配的字符串,并且使用最小的?号。
原题连接:http://codeforces.com/problemset/problem/412/C
#include <stdio.h>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
int main()
{
int N;
scanf("%d", &N);
vector<string> vst(N);
for (int i = 0; i < N; i++)
{
cin>>vst[i];
}
string ans;
for (int j = 0; j < (int)vst[0].size(); j++)
{
bool allX = true;
char x = vst[0][j];
for (int i = 1; i < (int)vst.size(); i++)
{
if (x == '?')
{
x = vst[i][j];
}
else if (vst[i][j] != '?' && vst[i][j] != x)
{
allX = false;
x = '?';
break;
}
}
if (allX && x == '?') ans.push_back('x');//全是?号
//else if (allX && x != '?') ans.push_back(x);//只有一个不是?号
else ans.push_back(x);//超过一个不是?号,和上面情况合并了
}
cout<<ans;
return 0;
}