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 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 2·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 test(s)
input
3
aAbB
output
0
input
4
aBaCaB
output
3
input
5
xYyXzZaZ
output
2

*****************************************************

题意说明:

n个房间排成一排,每个房间里有一把钥匙,房间之间有门,大写字母代表门小写字母代表钥匙,对应的字母可以打开对应的门,从左到右,要从第一个房间到达地n个房间,问需要买几把钥匙(到达一个房间可以拿到其中的钥匙,钥匙只能使用一次,如果手中没有能打开这个门的钥匙可以购买)。

******************************************************

这一题暴力来做,有 n^2 的算法,就是直接对于每个门检查手里的钥匙,判断是否购买;但是这样做超时,我第一次就是这样做的,没有经验就直接上了,结果超时。

有 O(n)  的算法,对于这样的只有大小写字母组成的字符串,并且是配对操作的基本上都有这样的一个做法,就是开一个26的数组,记录每个字幕的次数;

这一题中,碰到小写字幕就将小写字母数加一,碰到大写就检查是否有对应的小写字母,如果没有就要购买了,如果有,就减一,相当于消耗了一把该钥匙;

代码如下:

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
using namespace std;
char a[];
int n,ans=,t[]={};
int main()
{
scanf("%d",&n);
scanf("%s",a);
for(int i=;i<*n-;i+=)
{
t[a[i]-'a']++;
if(t[a[i+]-'A']<=)
ans++;
else
t[a[i+]-'A']--;
}
printf("%d\n",ans);
return ;
}

cf- 297 < a >--字符串操作技巧的更多相关文章

  1. shell字符串操作技巧

    操作字符串 -------------- Bash支持超多的字符串操作,操作的种类和数量令人惊异.但不幸的是,这些工具缺乏集中性. 一些是参数替换的子集,但是另一些则属于UNIX的expr命令.这就导 ...

  2. python 字符串 大小写转换 以及一系列字符串操作技巧

    总结 capitalize() 首字母大写,其余全部小写 upper() 全转换成大写 lower() 全转换成小写 title() 标题首字大写,如"i love python" ...

  3. shell在一个大文件找出想要的一段字符串操作技巧

    昨天端午,晚上的时候接了一个电话,我朋友的公司,数据库被两个工作没多久的phper给弄坏了,具体就是把一个字段值,给全表弄成一个了名字了,当然这个是可以配置了禁止全表更新数据库,这下可急坏了,找到我, ...

  4. OMG,12 个精致的 Java 字符串操作小技巧,学它

    字符串可以说是 Java 中最具有代表性的类了,似乎没有之一哈,这就好像直播界的李佳琪,脱口秀中的李诞,一等一的大哥地位.不得不承认,最近吐槽大会刷多了,脑子里全是那些段子,写文章都有点不由自主,真的 ...

  5. 《手把手教你》系列技巧篇(五十)-java+ selenium自动化测试-字符串操作-上篇(详解教程)

    1.简介 自动化测试中进行断言的时候,我们可能经常遇到的场景.从一个字符串中找出一组数字或者其中的某些关键字,而不是将这一串字符串作为结果进行断言.这个时候就需要我们对字符串进行操作,宏哥这里介绍两种 ...

  6. 《手把手教你》系列技巧篇(五十一)-java+ selenium自动化测试-字符串操作-下篇(详解教程)

    1.简介 自动化测试中进行断言的时候,我们可能经常遇到的场景.从一个字符串中找出一组数字或者其中的某些关键字,而不是将这一串字符串作为结果进行断言.这个时候就需要我们对字符串进行操作,宏哥这里介绍两种 ...

  7. Jquery数组操作技巧

    Jquery对数组的操作技巧. 1. $.each(array, [callback]) 遍历[常用]  解释: 不同于例遍 jQuery 对象的 $.each() 方法,此方法可用于例遍任何对象(不 ...

  8. Vi操作技巧

    Vi操作技巧: :nu    显示当前所在行的行号 :set nu    显示全部行号 :set nonu        取消显示行号 /字符串    查询字符串,按n查询下一个,按N查询上一个 持续 ...

  9. vim常用操作技巧与配置

    vi是linux与unix下的常用文本编辑器,其运行稳定,使用方便,本文将分两部分对其常用操作技巧和配置进行阐述,其中参考了网上的一些文章,对作者表示感谢 PART1 操作技巧 说明: 以下的例子中  ...

随机推荐

  1. 问题驱动的Git学习

    (搬运自我在SegmentFault的博客) 本人是个Git新手,平时用Git最多的就是push,因为别的都不怎么会用.这几天因为在小组中负责代码的整合,顺便将代码提交到Github,接触到了Git更 ...

  2. C#textbox允许换行

    要让一个TextBox显示多行文本就得把它的Multiline属性设置为true就行

  3. Nuget使用备忘

    菜单:工具-库程序包管理器-管理解决方案的NuGet程序包,搜索,下载,安装 或者 工具-库程序包管理器-程序包管理器控制台,输入PM命令,如: install-package log4net 如果不 ...

  4. 解决windows系统下打开应用弹出丢失libmysql.dll的问题

    只要把下载libmysql.dll,放到exe应用程序的所在目录,就可以运行,libmysql.dll有32位和64位版本,可以分别测试一下行不行,如果不行在换一个 版本试试.libmysql.dll ...

  5. 当c++遇上音乐

    运用到的函数 #include <windows.h> Beep( f, t ); Sleep( t ); eep() 函数可以让蜂鸣器发出频率为f赫兹,音长大约为 2t 毫秒的音.(注意 ...

  6. 【构造题 贪心】cf1041E. Tree Reconstruction

    比赛时候还是太慢了……要是能做快点就能上分了 Monocarp has drawn a tree (an undirected connected acyclic graph) and then ha ...

  7. Win10家庭版找不到组策略gpedit.msc

    首先在桌面上建立一个txt文本文件,将下面的代码复制到里面 @echo off pushd "%~dp0" dir /b C:\Windows\servicing\Packages ...

  8. 忘记root密码怎么办-单用户模式修改root密码

    忘记root密码怎么办-单用户模式修改root密码================================= 1,开机3秒内按下向下的方向键,目的是为了不让它进入系统,而是停留在开机界面. 2 ...

  9. mac Parallels Desttop 13 win7/win8 无法连接网络

    把 “源” 从共享网络改为"Wi-Fi" 在mac 这边点击菜单栏windows图标,选配置(如果没有配置,点控制中心,在点控制中心的齿轮,)选 硬件 找到网络 解锁,上边第一行就 ...

  10. DeepFaceLab小白入门(5):训练换脸模型!

    训练模型,是换脸过程中最重要的一部分,也是耗时最长的一部分.很多人会问到底需要多少时间?有人会告诉你看loss值到0.02以下就可以了.我会告诉你,不要看什么数值,看预览窗口的人脸.看第二列是否和第一 ...