2017-2018 ACM-ICPC, NEERC, Southern Subregional Contest E. Field of Wonders

E. Field of Wonders

time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Polycarpus takes part in the “Field of Wonders” TV show. The participants of the show have to guess a hidden word as fast as possible. Initially all the letters of the word are hidden.

The game consists of several turns. At each turn the participant tells a letter and the TV show host responds if there is such letter in the word or not. If there is such letter then the host reveals all such letters. For example, if the hidden word is “abacaba” and the player tells the letter “a”, the host will reveal letters at all positions, occupied by “a”: 1, 3, 5 and 7 (positions are numbered from left to right starting from 1).

Polycarpus knows m words of exactly the same length as the hidden word. The hidden word is also known to him and appears as one of these m words.

At current moment a number of turns have already been made and some letters (possibly zero) of the hidden word are already revealed. Previously Polycarp has told exactly the letters which are currently revealed.

It is Polycarpus’ turn. He wants to tell a letter in such a way, that the TV show host will assuredly reveal at least one more letter. Polycarpus cannot tell the letters, which are already revealed.

Your task is to help Polycarpus and find out the number of letters he can tell so that the show host will assuredly reveal at least one of the remaining letters.

Input
The first line contains one integer n (1 ≤ n ≤ 50) — the length of the hidden word.

The following line describes already revealed letters. It contains the string of length n, which consists of lowercase Latin letters and symbols ““. If there is a letter at some position, then this letter was already revealed. If the position contains symbol ““, then the letter at this position has not been revealed yet. It is guaranteed, that at least one letter is still closed.

The third line contains an integer m (1 ≤ m ≤ 1000) — the number of words of length n, which Polycarpus knows. The following m lines contain the words themselves — n-letter strings of lowercase Latin letters. All words are distinct.

It is guaranteed that the hidden word appears as one of the given m words. Before the current move Polycarp has told exactly the letters which are currently revealed.

Output
Output the single integer — the number of letters Polycarpus can tell so that the TV show host definitely reveals at least one more letter. It is possible that this number is zero.

Examples
input
4
a**d
2
abcd
acbd
output
2
input
5
lo*er
2
lover
loser
output
0
input
3
a*a
2
aaa
aba
output
1
Note
In the first example Polycarpus can tell letters “b” and “c”, which assuredly will be revealed.

The second example contains no letters which can be told as it is not clear, which of the letters “v” or “s” is located at the third position of the hidden word.

In the third example Polycarpus exactly knows that the hidden word is “aba”, because in case it was “aaa”, then the second letter “a” would have already been revealed in one of previous turns.

链接:http://codeforces.com/contest/883/problem/E

题意:
给一个字符串 T ,T中有部分为 ’*‘ 表示该位置是未知的,其余表示已知的。

现在你有 m 个单词,这些单词组合起来一定包含了正确答案。

问你存在多少个字符,使得这种字符一定能翻转至少一个 ’* ‘。

思路:
1.筛选掉一定错误的单词;
2.把筛选剩下的单词放到待测的集合里面;
3.遍历a到z,查看是否对于每个单词,都存在一个‘*’使得它能翻转得到。

#include <bits/stdc++.h>
using namespace std;
int cnt[256];
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int n,m;
    string t,s;
    cin>>n>>t>>m;

    int cnt[256]={0};
    int num=0;
    for(int i=0;i<n;i++)
    {
        if(t[i]!='*')
            cnt[t[i]]++;
        else num++;
    }

    vector<string> vec;
    while(m--)
    {
        cin>>s;
        int flag=1;
        for(int i=0;i<n;i++)
        {
            if(t[i]=='*')
            {
                if(cnt[s[i]])
                {
                    flag=0;break;
                }
            }
            else
            {
                if(s[i]!=t[i])
                {
                    flag=0;break;
                }
            }
        }
        if(flag)//flag==0时说明该串被筛选掉了
        {
            vec.push_back(s);
        }
    }
    int ans=0;

    for(int f='a';f<='z';f++)
    {
        int ff=1;
        for(auto tmp:vec)
        {
            int flag=0;
            for(int i=0;i<n;i++)
            {
                if(t[i]=='*'&&tmp[i]==f)
                {
                    flag=1;break;
                }
            }
            if(!flag)
            {
                ff=0;
                break;
            }
        }
        ans+=ff;
    }

    return cout<<ans,0;
}

转载请注明出处^ ^

猜你喜欢

转载自blog.csdn.net/mmingfunnytree/article/details/78306672
今日推荐