CodeForces 883E Field of Wonders

本文介绍了一种猜词游戏的策略,玩家需要根据已知线索猜测隐藏单词。通过分析已揭示的字母和候选词汇,确定哪些字母的选择能确保揭露更多隐藏字母。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description:

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.

Example
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.


题目大意:

题目略长读了半天也没看懂什么意思, 后来仔细读了一遍题。 题目大致意思就是给定一个保证是正确的字符串, 但是其中可能会有未知的字符, 现在给你m种提示, 最后判断再添加多少个字符可以变成一个不含未知字符的字符串。

解题思路:

读懂了题意看了一下数据量, 最多1001个字符串每个字符串长度不超过50, 给的时间竟然有三秒, 那么直接去枚举给的每一个提示的字符串去跟原始串匹配即可, 对于连已知位置的字符都不匹配的话可以直接跳过不用判断, 剩下的字符串中去查找某一字符是否在所有提示串中出现过, 如果出现过就ans++。

代码:

#include <iostream>
#include <sstream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <utility>
#include <string>
#include <cmath>
#include <vector>
#include <bitset>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>

using namespace std;

/*tools:
 *ios::sync_with_stdio(false);
 *freopen("input.txt", "r", stdin);
 */

typedef long long ll;
typedef unsigned long long ull;
const int dir[5][2] = {0, 1, 0, -1, 1, 0, -1, 0, 0, 0};
const ll ll_inf = 0x7fffffff;
const int inf = 0x3f3f3f;
const int mod = (int)1e9 + 7;
const int Max = (int) 1e3 + 7;

int n, m, ans;
bool vis[Max];
char s[Max], str[Max][Max];
set < char> st;

void Init() {
    cin >> s;
    for (int i = 0; i < strlen(s); ++i) {
        if (s[i] != '*') {
            st.insert(s[i]);
        }
    }
    cin >> m;
    memset(vis, 0, sizeof(vis));
    for (int i = 0; i < m; ++i) {
        cin >> str[i];
        // whether correctly string
        int flag = 1;
        for (int j = 0; j < strlen(str[i]); ++j) {
            if (s[j] == '*') {
                if (st.count(str[i][j])) {
                    flag = 0;
                    break;
                }
            } else if (s[j] != str[i][j]) {
                flag = 0;
                break;
            }
        }
        if (flag) vis[i] = 1;
    }
}

void check() {
    char ch[5];
    int ans = 0, flag;
    for (int i = 0; i < 26; ++i) {
        ch[0] = 'a' + i, ch[1] = '\0';
        // already exist
        if (st.count(ch[0])) continue ;
        flag = 1;
        for (int j = 0; j < m; ++j) {
            // do not exist
            if (vis[j] && strstr(str[j], ch) == NULL) {
                flag = 0;
                break;
            }
        }
        if (flag)
            ans++;
    }
    cout << ans << endl;
}

int main() {
    //freopen("input.txt", "r", stdin);

    ios::sync_with_stdio(false);
    // initialization
    cin >> n;
    Init();
    check();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值