Codeforces_round297_A.Vitaliy and Pie

来源:互联网 发布:淘宝 零食 知乎 编辑:程序博客网 时间:2024/05/22 15:37

英文原题:

After a hard day Vitaly got very hungry and he wants to eat his favorite potato pie. But it's not that simple. Vitaly is in the first room of the house withn room located in a line and numbered starting from one from left to right. You can go from the first room to the second room, from the second room to the third room and so on — you can go from the (n - 1)-th room to the n-th room. Thus, you can go to roomx only from room x - 1.

The potato pie is located in the n-th room and Vitaly needs to go there.

Each pair of consecutive rooms has a door between them. In order to go to roomx from room x - 1, you need to open the door between the rooms with the corresponding key.

In total the house has several types of doors (represented by uppercase Latin letters) and several types of keys (represented by lowercase Latin letters). The key of typet can open the door of type T if and only if t and T are the same letter, written in different cases. For example, keyf can open door F.

Each of the first n - 1 rooms contains exactly one key of some type that Vitaly can use to get to next rooms. Once the door is open with some key, Vitaly won't get the key from the keyhole but he will immediately run into the next room. In other words, each key can open no more than one door.

Vitaly realizes that he may end up in some room without the key that opens the door to the next room. Before the start his run for the potato pie Vitaly can buy any number of keys of any type that is guaranteed to get to roomn.

Given the plan of the house, Vitaly wants to know what is the minimum number of keys he needs to buy to surely get to the roomn, which has a delicious potato pie. Write a program that will help Vitaly find out this number.

Input

The first line of the input contains a positive integern (2 ≤ n ≤ 105) — the number of rooms in the house.

The second line of the input contains string s of length n - 2. Let's number the elements of the string from left to right, starting from one.

The odd positions in the given string s contain lowercase Latin letters — the types of the keys that lie in the corresponding rooms. Thus, each odd positioni of the given string s contains a lowercase Latin letter — the type of the key that lies in room number(i + 1) / 2.

The even positions in the given string contain uppercase Latin letters — the types of doors between the rooms. Thus, each even positioni of the given string s 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 keys that Vitaly needs to buy to surely get from room one to roomn.

 

        意思是说给有一排房n间,每个房里又有一枚钥匙,特定的钥匙开特定的门,规则是小写字母代表的钥匙开对应答谢字母表示的房间,一个钥匙只能开一次门,如果手里没有打开下 一扇门需要的钥匙就要买。

        给你字符串,奇数位置的为小写字母代表钥匙,偶数位置的为大写字母代表房间,求得就是需要额外买几把钥匙。样例如下:

Input
5xYyXzZaZ
Output
2

 

        数据不大,这个我们用一个int的钥匙数组模拟走的过程,其实就是当前手上26个小写字母的个数,见大一枚钥匙相应小写字母就++,遇到一间房对应钥匙的小写字母数为0则要买一个。

 

代码:

<span style="font-size:12px;color:#000000;">#include <iostream>#include<stdio.h>using namespace std;int n;//房间数string str;//输入字符串int keys[27]={0};//钥匙数组int cnt=0;//额外要购买的数量int main(){    cin>>n;    cin>>str;    for(int i=0;i<2*n-2;i++)    {        if(i%2==0)        {            keys[str[i]-97+1]++;        }else if(i%2==1)        {            if(keys[str[i]-65+1]==0)            {                cnt++;            }else            {                keys[str[i]-65+1]--;            }        }    }    printf("%d\n",cnt);    return 0;}</span>


 

 

 

 

 

0 0
原创粉丝点击