Shockers (Codeforces 906A)
Tags: Implement, strings

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

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.

Sample Inputs:
5
! abc
. ad
. b
! cd
? c

8
! hello
! codeforces
? c
. o
? d
? h
. l
? e

7
! ababahalamaha
? a
? b
? a
? b
? a
? h

Sample Outputs:
1

2

0

Solution (Python):
s = set([chr(i) for i in range(ord('a'), ord('z') + 1)])
cnt = 0
for i in range(int(input()) - 1):
    ss = input()
    if ss[0] != '.' and len(s) == 1:
        cnt += 1
    if ss[0] == '!':
        s &= set(ss[2:])
    else:
        s -= set(ss[2:])
print(cnt)

Solution (Python):
n = int(input())
c = [1] * 26
s = 0
l = 0
for i in range(n):
    a = input()
    if l == 0:
        if a[0] == '.':
            for j in a[2:]:
                c[ord(j) - ord('a')] = 0
        elif a[0] == '?':
            c[ord(a[2]) - ord('a')] = 0
        else:
            d = 0
            for j in range(26):
                f = 0
                for u in range(2, len(a)):
                    if ord(a[u]) - ord('a') == d:
                        f = 1
                        break
                d += 1
                if f == 0:
                    c[j] = 0
        if sum(c) == 1 and l == 0:
            l = 1
    elif a[0] == '!' or (i != n - 1 and a[0] == '?'):
        s += 1
print(s)

Solution (Python):
n = int(input())
a = [-1] * 26
all = set((i for i in 'abcdefghijklmnoprqstuvwxyz'))
bad = set('')
res_cnt = 0
flag = False
for i in range(n - 1):
    (cmd, word) = input().split()
    if flag:
        if cmd in '!?':
            res_cnt += 1
        continue
    if cmd == '.':
        for c in set(word):
            pos = ord(c) - ord('a')
            a[pos] = 0
            all.discard(c)
            bad.discard(c)
    elif cmd == '!':
        curr_bad = set()
        for c in set(word):
            pos = ord(c) - ord('a')
            if a[pos] == 0:
                all.discard(c)
            else:
                curr_bad.add(c)
        if not bad:
            bad = curr_bad
        else:
            old_bad = bad.copy()
            bad = bad.intersection(curr_bad)
            for c in old_bad - bad:
                pos = ord(c) - ord('a')
                a[pos] = 0
                all.discard(c)
            for c in curr_bad - bad:
                pos = ord(c) - ord('a')
                a[pos] = 0
                all.discard(c)
    else:
        c = word[0]
        pos = ord(c) - ord('a')
        a[pos] = 0
        all.discard(c)
        bad.discard(c)
    if len(bad) == 1 or len(all) == 1:
        flag = True
print(res_cnt)