CF412C Pattern 题解
Content
有 \(n\) 个长度均为 \(s\) 的字符串,每个字符串都由小写字母和 \(?\)(表示这个字符不确定) 表示,求一个能使 \(n\) 个字符串的每一位的字母均相等,如果有不符合规定的,则这个字符输出 \(?\)。你需要尽可能的使 \(?\) 的数量最小。
数据范围:\(1\leqslant n\leqslant 10^5,1\leqslant s\leqslant 10^5\)。
Solution
每个字符串逐个字符去匹配,如果有不匹配的就用 \(?\) 以外的其他字符表示(下面代码中用 \(!\) 表示),其他的如果有不确定的随便弄个字母(下面的代码中使用的是 \(\text{z}\) 这个字母),就可以过了。
Code
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
using namespace std;
int n;
string a[100007];
int main() {
scanf("%d", &n);
cin >> a[1];
for(int i = 2; i <= n; ++i) {
cin >> a[i];
for(int j = 0; j < a[i].size(); ++j) {
if(a[1][j] == '?')
a[1][j] = a[i][j];
else if(a[1][j] == '!') continue;
else if(a[1][j] != a[i][j] && a[i][j] != '?') {
// puts("Wrong: No");
// printf("Wrong on text %d\n", j);
a[1][j] = '!';
// printf("%c\n", a[1][j]);
}
}
}
for(int i = 0; i < a[1].size(); ++i) {
// printf("%c %d\n", a[1][i], a[1][i] == '!');
if(a[1][i] == '!') printf("?");
else if(a[1][i] == '?') printf("z");
else printf("%c", a[1][i]);
}
return 0;
}