题目分类:普通模拟
题目描述:n-1个房间,依次编号为1到n-1,打开第i号房间门之前需要先打开前i-1号房间门。每个房间门前有一把特别的钥匙可以打开对应的门且只能使用一次。如果在开门之前没有钥匙需要借钥匙开门,问最少借几次钥匙。给2*(n-1)个小大写相间的英文字母,小写字母对应钥匙,大写字母对应门。钥匙可以打开相应大写字母对应的门。
解题思路:数组f[i]代表第i个小写字母对应的钥匙目前有多少把,ans代表答案。若拥有开当前门的钥匙,令对应的钥匙数量减一;否则ans++。
算法复杂度:O(n)。
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
usingnamespace std;
constint N= 200010;
char s[N];
int b[30];
intmain()
{
int n;
while(~scanf("%d",&n))
{
scanf("%s",s);
int ans=0;
memset(b,0,sizeof(b));
for(int i=0;s[i]!='\0';i+=2)
{
b[s[i]-'a']++;
if(b[s[i+1]-'A']==0)
{
ans++;
}else b[s[i+1]-'A']--;
}
printf("%d\n",ans);
}
return0;
}
原题描述:
A. Vitaliy and Pie
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
After a hard day Vitaly got very hungry and he wantsto eat his favorite potato pie. But it's not that simple. Vitaly is in thefirst room of the house with n room located in a line and numberedstarting from one from left to right. You can go from the first room to thesecond room, from the second room to the third room and so on — you can go fromthe (n - 1)-th roomto the n-th room. Thus, you can go to roomx only from roomx - 1.
The potato pie is located in then-th room andVitaly needs to go there.
Each pair of consecutive rooms has a door betweenthem. In order to go to roomx from roomx - 1, you need to open the door between the rooms withthe corresponding key.
In total the house has several types of doors(represented by uppercase Latin letters) and several types of keys (representedby lowercase Latin letters). The key of type t can open the door of typeT if and only if t andT are the same letter, written indifferent cases. For example, key f can open door F.
Each of the firstn - 1 rooms contains exactly one key of some type that Vitaly can use to getto next rooms. Once the door is open with some key, Vitaly won't get the keyfrom the keyhole but he will immediately run into the next room. In otherwords, each key can open no more than one door.
Vitaly realizes that he may end up in some roomwithout the key that opens the door to the next room. Before the start his runfor the potato pie Vitaly can buy any number of keys of any type that isguaranteed to get to roomn.
Given the plan of the house, Vitaly wants to know whatis the minimum number of keys he needs to buy to surely get to the room n,which has a delicious potato pie. Write a program that will help Vitaly findout this number.
Input
The first line of the input contains a positiveintegern (2 ≤ n ≤ 105) — the number of rooms in thehouse.
The second line of the input contains stringsof length 2·n - 2. Let'snumber the elements of the string from left to right, starting from one.
The odd positions in the given strings containlowercase Latin letters — the types of the keys that lie in thecorresponding rooms. Thus, each odd positioni of the given stringscontains a lowercase Latin letter — the type of the key that lies in roomnumber (i + 1) / 2.
The even positions in the given string containuppercase Latin letters — the types of doors between the rooms. Thus, each evenposition i of the given strings contains an uppercase letter —the type of the door that leads from roomi / 2 to room i / 2 + 1.
Output
Print the only integer — the minimum number of keysthat Vitaly needs to buy to surely get from room one to room n.
Sample test(s)
Input
3
aAbB
Output
0
Input
4
aBaCaB
Output
3
Input
5
xYyXzZaZ
Output
2