Shockers
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.
OutputOutput 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.
Examples5 ! abc . ad . b ! cd ? c
1
8 ! hello ! codeforces ? c . o ? d ? h . l ? e
2
7 ! ababahalamaha ? a ? b ? a ? b ? a ? h
0
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.
“. word”:选手说出的单词中不包含该字母
“! word”:选手说出的单词中包含该字母
“? letter”:选手猜了某一个字母,这个行为只在最后一次是正确的
在最后一次之前,每有一个‘!‘或‘?‘,选手都会被电一次。在某个时刻起,答案就唯一确定了,问从那个时刻起到最后,选手被电了多少次?
思路:如果一串字符被电击了,那么这一串中一定包含那个被选中的字母,我们并不好判断,但是可以肯定的是除去这一字符串中所包含的字母全是未被选中,我们通过标记未被选中的字母,最终看啥时候未被选中的字母我们已经标记了25个说明我们找出了那个被选中的,在这之后的感叹号和问号(猜的未被选中的字符)都是多余出来的电击
code:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e5+10;
char s[maxn];
int safe[30],nowsafe[30];
int main(){
int n,i,j,flag = 0,ans = 0;
char c;
cin >> n;
while(n--){
cin >> c >> s;
int len = strlen(s);
if(c == '!'){
if(flag == 1) ans++;//flag=1说明已经确定了字母,再电击就是多余的
else{//如果还没判断出来
fill(nowsafe,nowsafe+26,1);//假设全部安全
for(i = 0; i < len; i++){
nowsafe[s[i]-'a'] = 0;
}//因为还无法判断,就把这一串中包含的字母都不安全
for(i = 0; i < 26; i++){
if(nowsafe[i] == 1)
safe[i] = 1;//因为只有一个被选中的字母,所以除了被电击的字符串中的字母其他字母一定不是被选中的也就是安全的
}
}
}
else if(c == '.'){
for(int i = 0; i < len; i++){
safe[s[i]-'a'] = 1;//没有电击的字符串中的字母一定都是没被选中的所以都是安全的
}
}
else{//问号
if(flag == 1 && safe[s[0]-'a'] == 1)
ans++;//如果已经确定了被选中字母,并且猜的不是被选中的,得到的电击是多余的
safe[s[0]-'a'] = 1;//因为除了最后一个问号,前面猜测全不对,所以把它设为安全
}
int num = 0;
for(int i = 0; i < 26; i++){
if(safe[i] == 1) num++;
}
if(num == 25) flag = 1;
}
cout << ans << endl;
return 0;
}