题目链接: https://codeforces.com/problemset/problem/907/C
题意:
给定 n 种字符串,三种不同的形式,! 表示受罚,. 表示不受罚,? 表示猜测。保证最后一次才猜对。问能少被惩罚多少次。
思路:
纯模拟,对于! 串取交集,对. ?串取差集。建立一个待定字符集。每次按照题目要求进行待定集的修改,当待定字母数成为 1 之后的操作都是多余的。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| #include<bits/stdc++.h> using namespace std; #define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); const int N=50; int a[N]; void solve() { int n; cin>>n; for(int i=0;i<26;i++)a[i]=1; int num=26; int ans=0; for(int i=1;i<=n;i++) { char op; cin>>op; string str; cin>>str; if(op=='.'){ for(int j=0;j<str.size();j++) { if(a[str[j]-'a']) { a[str[j]-'a']=0; num--; } } } else if(op=='!') { if(num==1)ans++; int temp[26]={0}; for(int j=0;j<str.size();j++) { temp[str[j]-'a']=1; } for(int j=0;j<26;j++) { if(a[j]&&temp[j]==0) { a[j]=0; num--; } } }else { if(i!=n){ if(num==1)ans++; if(a[str[0]-'a'])num--; a[str[0]-'a']=0; } } } cout<<ans; } signed main(){ IOS int t=1; while(t--) { solve(); } }
|