cf 906A Shockers

一 原题

C. Shockers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Valentin participates in a show called "Shockers". The rules are quite easy: jury selects one letter which Valentin doesn't know. He should make a small speech, but every time he pronounces a word that contains the selected letter, he receives an electric shock. He can make guesses which letter is selected, but for each incorrect guess he receives an electric shock too. The show ends when Valentin guesses the selected letter correctly.

Valentin can't keep in mind everything, so he could guess the selected letter much later than it can be uniquely determined and get excessive electric shocks. Excessive electric shocks are those which Valentin got after the moment the selected letter can be uniquely determined. You should find out the number of excessive electric shocks.

Input

The first line contains a single integer n (1 ≤ n ≤ 105) — the number of actions Valentin did.

The next n lines contain descriptions of his actions, each line contains description of one action. Each action can be of one of three types:

  1. Valentin pronounced some word and didn't get an electric shock. This action is described by the string ". w" (without quotes), in which "." is a dot (ASCII-code 46), and w is the word that Valentin said.
  2. Valentin pronounced some word and got an electric shock. This action is described by the string "! w" (without quotes), in which "!" is an exclamation mark (ASCII-code 33), and w is the word that Valentin said.
  3. Valentin made a guess about the selected letter. This action is described by the string "? s" (without quotes), in which "?" is a question mark (ASCII-code 63), and s is the guess — a lowercase English letter.

All words consist only of lowercase English letters. The total length of all words does not exceed 105.

It is guaranteed that last action is a guess about the selected letter. Also, it is guaranteed that Valentin didn't make correct guesses about the selected letter before the last action. Moreover, it's guaranteed that if Valentin got an electric shock after pronouncing some word, then it contains the selected letter; and also if Valentin didn't get an electric shock after pronouncing some word, then it does not contain the selected letter.

Output

Output a single integer — the number of electric shocks that Valentin could have avoided if he had told the selected letter just after it became uniquely determined.

Examples
input
5
! abc
. ad
. b
! cd
? c
output
1
input
8
! hello
! codeforces
? c
. o
? d
? h
. l
? e
output
2
input
7
! ababahalamaha
? a
? b
? a
? b
? a
? h
output
0
Note

In the first test case after the first action it becomes clear that the selected letter is one of the following: a, b, c. After the second action we can note that the selected letter is not a. Valentin tells word "b" and doesn't get a shock. After that it is clear that the selected letter is c, but Valentin pronounces the word cd and gets an excessive electric shock.

In the second test case after the first two electric shocks we understand that the selected letter is e or o. Valentin tries some words consisting of these letters and after the second word it's clear that the selected letter is e, but Valentin makes 3 more actions before he makes a correct hypothesis.

In the third example the selected letter can be uniquely determined only when Valentin guesses it, so he didn't get excessive electric shocks.


二 分析

事先设定一个小写字母c作为答案,你每次可以:1)说出一个字符串,如果字符串包含c,则被shock一次;2)说出你认为是c的一个字母,如果猜错,被shock一次。输入保证最后一次一定是行为2),且猜测正确。

问有多少次shock是不必要的,即c已经唯一确定,但你还被shock了。。简单模拟,之前不知道c++还可以写range-based loop,原来c++11就引入了。。


三 代码

运行结果:
LANG: GNU C++14
Verdict: Accepted

AC代码:
#include
#include
#include

using namespace std;

// #define LOCAL

enum State {
    unknown, safe
};

char c;
string s;
State state[26];
bool appear[26], sure = false;
int n, ans;

inline void check() {
    if(sure) {
        return;
    }
    int cnt = 0;
    for(int i = 0; i < 26; i++) {
        if(state[i] == unknown) {
            cnt++;
        }
    }
    if(cnt == 1) {
        sure = true;
    }
}

int main() {
    #ifdef LOCAL
    freopen("1.in", "r", stdin);
    #endif
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    cin >> n;
    while(n--) {
        cin >> c >> s;
        if(c == '.') {
            for(char cc: s) {
                state[cc - 'a'] = safe;
            }
        }
        else if(c == '?') {
            if(sure && n != 0) {
                ans++;
            }
            else {
                state[s[0] - 'a'] = safe;
            }
        }
        else {
            if(sure) {
                ans++;
            }
            else {
                memset(appear, 0, sizeof(appear));
                for(char cc: s) {
                    appear[cc - 'a'] = true;
                }
                for(int i = 0; i < 26; i++) {
                    if(!appear[i]) {
                        state[i] = safe;
                    }
                }
            }
        }
        check();
    }
    cout << ans << endl;
    return 0;
}


你可能感兴趣的:(cf,模拟)