CF525A.Vitaliy and Pie

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

经过辛苦的一天后,维塔利感到非常饥饿,他想吃自己最爱的土豆派。但事情并不那么简单。维塔利位于一栋共有 nn 个房间的房子里的第一个房间,这些房间排成一条直线,从左到右依次编号为 1,2,…,n1, 2, \dots, n。他可以从第 11 个房间走到第 22 个房间,从第 22 个房间走到第 33 个房间,依此类推——即只能从第 (n−1)(n-1) 个房间走到第 nn 个房间。因此,维塔利只能从第 x−1x-1 个房间进入第 xx 个房间。

土豆派位于第 nn 个房间,维塔利必须到达那里。

每对相邻房间之间都有一扇门。要从第 x−1x-1 个房间进入第 xx 个房间,必须用对应的钥匙打开这两房间之间的门。

整栋房子中存在多种类型的门(用大写拉丁字母表示)和多种类型的钥匙(用小写拉丁字母表示)。类型为 tt 的钥匙能打开类型为 TT 的门,当且仅当 tt 和 TT 是同一字母的大小写形式。例如,钥匙 f 可以打开门 F。

前 n−1n-1 个房间中,每个房间恰好包含一把某种类型的钥匙,维塔利可利用这些钥匙前往后续房间。一旦用某把钥匙打开了门,维塔利不会从锁孔中取回该钥匙,而是立即冲入下一个房间。换言之,每把钥匙最多只能打开一扇门。

维塔利意识到,他可能会到达某个房间,却缺少打开通往下一房间之门所需的钥匙。在开始奔向土豆派之前,维塔利可以购买任意数量、任意类型的钥匙,以确保一定能到达第 nn 个房间。

给定房屋的布局,维塔利想知道:为确保一定能够到达存放美味土豆派的第 nn 个房间,他最少需要购买多少把钥匙?请编写一个程序,帮助维塔利求出这个最小数量。

输入格式

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 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.

输入的第一行包含一个正整数 nn(2≤n≤1052 \leq n \leq 10^5)—— 表示房屋中的房间数量。

输入的第二行包含一个长度为 2⋅n−22\cdot n - 2 的字符串 ss。我们从左到右对字符串的元素编号,起始编号为 1。

给定字符串 ss 中所有奇数位置上为小写拉丁字母——表示对应房间中钥匙的类型。因此,字符串 ss 的每个奇数位置 ii 上的小写拉丁字母,即为房间编号 i+12\frac{i+1}{2} 中所放置钥匙的类型。

给定字符串 ss 中所有偶数位置上为大写拉丁字母——表示房间之间门的类型。因此,字符串 ss 的每个偶数位置 ii 上的大写字母,即为连接房间 i2\frac{i}{2} 与房间 i2+1\frac{i}{2} + 1 的门的类型。

输出格式

Print the only integer — the minimum number of keys that Vitaly needs to buy to surely get from room one to room n.

输出唯一的整数——Vitaly 为确保能从房间 1 到达房间 nn 所需购买的最少钥匙数量。

输入输出样例

  • 输入#1

    3
    aAbB

    输出#1

    0
  • 输入#2

    4
    aBaCaB

    输出#2

    3
  • 输入#3

    5
    xYyXzZaZ

    输出#3

    2

输入解题思路,AI测评打分。不知道怎么写?

首页