每日一题G

这几天做的题老是时间超时,弄得我都崩溃了,请教别人之后这才知道自己数组开的空间太小了。这个题是把大小写字母分别存在两个数组。一开始我做这个题是用两个for循环外层是大写字母,内层是小写字母。大写字母找小写字母,用i,j,控制(用“门”之前的"钥匙").找到的话把小写字母变成对应的大写字母,这样就不会下次找钥匙的时候再找到它。可是运行结果超时。

于是得到第二种算法,再设一个数组存放钥匙,核心思想就是把钥匙放在固定的门处(就是看看你所有钥匙放在你对应的门处,如果有钥匙门打开,钥匙用了减去。如果没有那么买一把)

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 i of 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
3
aAbB
Output
0
Input
4
aBaCaB
Output
3
Input
5
xYyXzZaZ
Output
2
#include <stdio.h>
#include <string.h>

int main()
{
    char a[2000020],b[2000020],c[2000020];
    int d[100]={0};
    int n,i,j,t,count1=0,count2=0,count=0;
   scanf("%d",&n);
   scanf("%s",c);
   t=strlen(c);
   for(i=0;i<t;i+=2)
   {

            a[count1]=c[i];
            count1++;
   }
   for(j=1;j<t;j+=2)
   {
         b[count2]=c[j];
            count2++;

   }
  for(i=0;i<count2;i++)
  {     d[a[i]-'a']++;
     if(d[b[i]-'A']==0)
        count++;
     else
        d[b[i]-'A']--;
  }
  printf("%d",count);
    return 0;
}

在这里我用了strlen 函数计算输入字符串大小,用变量保存,下次再用不知道是不是可以提高效率,望大家给我建议。

还有输入大小写字母时可以分奇数偶数输入

for(i=0;i<strlen(c);i++)
   {
       if(i%2==0||i==0)
       {
            a[count1]=c[i];
            count1++;
       }
       if(i%2!=0)
        {
            b[count2]=c[i];
            count2++;
        }
   }

不知道这段代码是不是比前面程序慢一点。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值