每日一题G 2015/6/3 16:38

来源:互联网 发布:郑州php培训哪家好 编辑:程序博客网 时间:2024/05/05 17:43

终于终于把这几道小题弄完了,心好累。这个小破题害我花了4个半小时才AC(之间也干别的事了,比如吃饭,看看新闻)这个一开始 我想的是先把门和钥匙分离出来,变成两个数组方便计算,然后当指向第i个门时,遍历整个第i个(包括第i个)前所有的钥匙,如果没找到就cnt++,如果找到了就把代表钥匙的字母变成0,因为题干说一个钥匙只能用一次,后来提交的时候我就想可能会在面对比较大的数据时 运行超时,果真超时了。因为for的嵌套,于是就想办法化简当前的代码,化简了几次还是运行超时,因为导致超时的for嵌套没有大的改变,但这个算法本身没有什么大问题,于是换一个思路,找一个数组来对应配套的门和钥匙。于是用if判断代替了遍历钥匙数组的那层循环,结果又是在那个大数据的时候(我猜那个肯定是个大数据,因为之前都是在那里超的时)WA了,急的我不行,以为算法出了问题,于是又缜密的推理了几遍确定无误,开始找代码的bug,我看S数组开的比较大,于是乎,放到了main外面,提交,惊喜来了,提示RE,我看看开的数组大小10的5次方加5 在有效数据的范围内啊。又错了几次抱着试试看的心态又加了个0,开到10的6次方,结果AC了。。。。。这不是坑爹吗?

#include<stdio.h>#include<math.h>#include<stdio.h>int main(){    int n;    scanf("%d", &n);    char s[1000005];    int str1[1000005];    int str2[1000005];    int ok[50]={0};    scanf("%s", s);    int i;    int j;    int cnt = 0;    for(i =0 ,j = 0; j < n - 1; i += 2, j++)    {        str1[j] = s[i];//读入小写        //printf("%c    ",str1[j]);    }    for(i =1 ,j = 0; j < n - 1; i += 2, j++)    {        str2[j] = s[i];       // printf("%c    ",str2[j]);    }    for(i = 0 ; i < n-1 ; i++)    {        ok[str1[i] - 'a']++;        if(ok[str2[i]-'A'] == 0)            cnt++;        else            ok[str2[i]- 'A']--;    }    printf("%d", cnt);    return 0;}


Description

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 with n 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 room x 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 room x 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 type t can open the door of type T if and only if t and T are the same letter, written in different cases. For example, key f 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 room n.

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 room n, 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 integer n (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 position i 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 position iof the given string s contains an uppercase letter — the type of the door that leads from room i / 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 room n.

Sample Input

Input
3aAbB
Output
0
Input
4aBaCaB
Output
3
Input
5xYyXzZaZ
Output
2


0 0
原创粉丝点击