题意:猜单词游戏,每次可以报一个字母(a~z且还未在已知字符串中出现),如果字符串中有这个字母,则将此串中所有字母显现;现在参赛者已知一些字符串(必有正确答案),问可以说出几个字母,是的至少有一个字母能够被显现出来。
分析:
1、筛选待选字符串,将与” * “对应并且该位置字母在已知串中出现过,删除(原串a*cd,待选aacd);不是” * “的位置对应的字母不相同的,删除(原串aadc * ,待选aadab,不要)。
2、若要确保有至少有一个显现出来,那么必定这个字母在所有的待选串中都出现过。
因此只需要对26个字母,遍历每一个待选串,并且记录是否全部都有。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <cctype>
#include <fstream>
#define INF 0x3f3f3f3f
#define EPS 1e-6
#define TEST cout<<"stop here"<<endl
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
string s;
int cnt[30];
bool is_star[60];
int n,m;
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(0);
//int T;
//cin>>T;
while(cin>>n>>s>>m){
memset(cnt,0,sizeof(cnt));
memset(is_star,false,sizeof(is_star));
for(int i=0;i<n;i++){
if(s[i]!='*'){
cnt[s[i] - 'a'] ++;
}
else{
is_star[i] = true;//记录一下第 j 个是不是
}
}
vector<string> rec;//待选字符串
for(int i=0;i<m;i++){
string st;
bool ok = false;
cin>>st;
for(int j=0;j<n;j++){
if(is_star[j]){
if(cnt[st[j] - 'a']){
ok = true;break;
}
}
else{
if(s[j] != st[j]){
ok = true;break;
}
}
}
if(!ok){
rec.push_back(st);
}
}
int ans = 0;
bool all[30][1010];
memset(all,false,sizeof(all));
for(int i=0;i<26;i++){
for(int j=0;j<rec.size();j++){//遍历所有串找所有串都有的字母
for(int k=0;k<n;k++){
if(is_star[k] && rec[j][k] == 'a' + i){
all[i][j] = true;break;//第 j 个答案有没有 字母 i
}
}
}
}
for(int i=0;i<26;i++){
bool ok = false;
for(int j=0;j<rec.size();j++){
if(!all[i][j]){
ok = true;break;
}
}
if(!ok)ans++;
}
cout<<ans<<endl;
}
return 0;
}