Codeforces 906A Shockers (模拟)

 

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

题目链接:http://codeforces.com/problemset/problem/906/A

 

题意:jury给了一个字符,Valentin 需要去猜这个正确的字符是什么,然后给出n行,每行的含义如下:

 

! word  : 表示当前的语句中包含要正确的字符‘’

. word :表示当前的语句中不包含要找的正确的字符

    ? lettet:表示猜的字符,,只有最后一次猜的字符才是对的。

求根据这些条件,判断在确定正确的字符后,Valentin 被电击的次数是多少?(也就是说根据已知条件如果能够判断出正确字符是什么,求出剩下的被电击的次数)

思路: 对于  .    后的字符串,则都不符合条件,?后的字符没必要判断,所以只要判断!后的字符即可,vis[]记录符合条件的字符出现的字符,不符合条件的字符的值为-1,每次只要把出现次数最多的字符和!后的字符串中的字符比较,看是否出现。当!出现x次,如果当前vis[]中有字符出现x次且只有一个字符,则这个字符就是符合条件的正确字符。

 

#include <bits/stdc++.h>
using namespace std;

const int N = 1e5+10;
int n,vis[30],cnt,flag,ans,cnta;
char str[N],ch[3];

int main(){
    scanf("%d",&n);
    for(int i = 0; i < n; i ++){
        scanf("%s%s",ch,str);
        int len = strlen(str);
        if(ch[0] == '.'){
            for(int i = 0; i < len; i ++) vis[str[i] - 'a'] = -1;
        }
        else if(ch[0] == '!'){
            if(flag) ans ++;
            else{
                cnta ++;
                int MaxValue = 0;
                for(int i = 0; i < 26; i ++) MaxValue = max(MaxValue,vis[i]);
                for(int i = 0; i < 26; i ++){
                    if(vis[i] == MaxValue){
                        int j = 0;
                        for(; j < len; j ++){
                            if(str[j] == i + 'a'){
                                vis[i] ++;
                                break;
                            }
                        }
                        if(j == len) vis[i] = -1;
                    }
                    else{
                        vis[i] = -1;
                    }
                }
            }
        }
        else{
            vis[str[0] - 'a'] = -1;
            if(flag) ans ++;
        }
        cnt = 0;
        for(int i = 0; i < 26; i ++) if(vis[i]>= cnta) cnt ++;
        if(cnt == 1) flag = 1;
    }
    printf("%d\n",ans ? ans-1 : ans);
    return 0;
}

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值