题目大意:小写字母是钥匙,大写字母是门,字母相同的钥匙能开对应的门,从第一个门走到最后一个门,问至少要配多少钥匙才能走到最后
思路:扫一遍记录当前拥有的钥匙即可,如果手上没有这种钥匙那么配一把
1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #define maxn 200000 6 char ch[maxn]; 7 using namespace std; 8 int key[maxn]; 9 int main() 10 { 11 int n,ans=0; 12 scanf("%d",&n); 13 scanf("%s",ch+1); 14 for(int i=1;i<=n-1;i++) 15 { 16 key[ch[(i-1)*2+1]-'a']++; 17 int u=ch[(i-1)*2+2]-'A'; 18 if(key[u]>0)key[u]--;else ans++; 19 } 20 printf("%d\n",ans); 21 }