Shockers (Codeforces 907C)
Tags: Implement, bitmasks

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):
import math, sys, bisect, heapq
from collections import defaultdict, Counter, deque
from itertools import groupby, accumulate
int1 = lambda x: int(x) - 1
input = iter(sys.stdin.buffer.read().decode().splitlines()).__next__
ilele = lambda : map(int, input().split())
alele = lambda : list(map(int, input().split()))
ilelec = lambda : map(int1, input().split())
alelec = lambda : list(map(int1, input().split()))

def list2d(a, b, c):
    return [[c] * b for i in range(a)]

def list3d(a, b, c, d):
    return [[[d] * c for j in range(b)] for i in range(a)]

def Y(c):
    print(['NO', 'YES'][c])

def y(c):
    print(['no', 'yes'][c])

def Yy(c):
    print(['No', 'Yes'][c])
extra = -1
s = set()
mark = {}
for _ in range(int(input())):
    (a, b) = input().split(' ')
    if len(s) == 1 or len(mark) == 25:
        if a != '.':
            extra += 1
        continue
    if a == '!':
        if not s:
            s = set(b)
        else:
            c = set(b)
            l = []
            for i in c:
                if i in s:
                    l.append(i)
            new_s = set()
            for i in s:
                if i in l:
                    new_s.add(i)
                else:
                    mark[i] = 1
            s = new_s.copy()
    elif a == '.':
        c = set(b)
        l = []
        for i in c:
            mark[i] = 1
            if i in s:
                l.append(i)
        new_s = set()
        for i in s:
            if i not in l:
                new_s.add(i)
        s = new_s.copy()
    else:
        mark[b] = 1
    t = set()
    for i in s:
        if mark.get(i, -1) == -1:
            t.add(i)
    s = t.copy()
print(max(0, extra))

Solution (Python):
import sys
sys.setrecursionlimit(1000000)
read = sys.stdin.readline
q = int(read())
count = 0
psw = set(list('qwertyuiopasdfghjklzxcvbnm'))
unique = None
for _ in range(q):
    (s, w) = read().strip().split()
    if len(psw) != 1:
        if s == '!':
            _psw = set()
            for _w in w:
                if _w in psw:
                    _psw.add(_w)
            psw = _psw
        elif s == '.':
            for _w in w:
                psw.discard(_w)
        elif s == '?':
            psw.discard(w)
    if unique is None and len(psw) == 1:
        unique = list(psw)[0]
        continue
    if unique is not None:
        if s == '!':
            count += 1
        elif s == '?' and w != unique:
            count += 1
print(count)

Solution (Python):
import sys
f = sys.stdin
n = int(f.readline())
cset = set((chr(ord('a') + i) for i in range(26)))
found = False
res = 0
for _ in range(n):
    (action, word) = f.readline().split()
    if action == '!':
        if found:
            res += 1
        cset &= set(word)
    elif action == '.':
        cset -= set(word)
    else:
        if word in cset:
            cset.remove(word)
        if found and len(cset) == 1:
            res += 1
    if len(cset) == 1:
        found = True
print(res)