Codeforces Round #297 (Div. 2) —— A. Vitaliy and Pie
来源:互联网 发布:数据库查询算法 编辑:程序博客网 时间:2024/06/06 03:44
题意:n个房间,每两个相邻的房间之间有一道门,门的种类也许不同,所以对于不同的门要用相应的钥匙才能打开,并且只有打开房间i-1才能到房间i,不能跨越多个房间,如果不能打开当前门,可以购买对应的一个钥匙,直到到达最后一个门为止,问所需买的最小的钥匙数量,其中小写字母代表钥匙,大写字母代表门,n个房间,2*n-2个字符串长度。
解题思路:起初用暴力,结果超时了,后面看了解题报告重新写了一份过了,在此以我的理解方式再给大家阐述下解题过程:
利用哈希表进行模拟。因为每个小写字母-‘a’与每个大写字母-‘A’相等,并且小写字母都在偶数为止,大写字母在奇数位置,所以将偶数位置上的小写字母-‘a’的差值哈希到数组中,然后大写字母-‘A’进行查找,如果对应位置不为空,则不需购买,否则需购买钥匙,详见代码:
Code:
#include <iostream>#include <cstdio>using namespace std;#define MAX 200000+10int Map[35];char str[MAX];int main(){ int n; while(~scanf("%d",&n)) { int cnt = 0; scanf("%s",str); for(int i = 0; i<2*n-2; i++) { if(i%2==0) Map[str[i]-'a']++;//将小写字母代表的钥匙哈希进数组Map中 else { if(Map[str[i]-'A'] > 0) //如果大写字母与A的差值在数组中的哈希位置不为空,则说明有钥匙可以打开此门 Map[str[i]-'A']--; else cnt++;//否则要购买钥匙即cnt++ } } printf("%d\n",cnt); } return 0;}
0 0
- Codeforces Round #297 (Div. 2) —— A. Vitaliy and Pie
- Codeforces Round#297 A.Vitaliy and Pie
- A. Vitaliy and Pie(Codeforces Round #297 (Div. 2) 水题)
- Codeforces Round #297 (Div. 2) A - Vitaliy and Pie 解题报告
- Codeforces #297 (Div. 2) A. Vitaliy and Pie (水
- #297 (div.2) A. Vitaliy and Pie
- A. Vitaliy and Pie
- A. Vitaliy and Pie
- A. Vitaliy and Pie
- CodeForces 525A Vitaliy and Pie 题意题
- CF 525A(Vitaliy and Pie-模拟)
- coderforce 525A Vitaliy and Pie
- Codeforces_round297_A.Vitaliy and Pie
- Vitaliy and Pie
- Codeforces Round #243 (Div. 2)——A. Sereja and Mugs
- Codeforces Round #223 (Div. 2)——A. Sereja and Dima
- Codeforces Round #250 (Div. 2)—A. The Child and Homework
- Codeforces Round #297 (Div. 2) A(模拟)
- IOS 问题总结一 frame 和scrollView
- Android获取图片资源之——拍照后在程序中显示照片
- P51 第八题
- Unity3D - 图形性能优化:渲染统计窗口
- 正则表达式的应用
- Codeforces Round #297 (Div. 2) —— A. Vitaliy and Pie
- XML跨平台,你懂的
- hdu4283 区间dp
- 水NOJ FREETALK的小游戏
- ios MKMapView的简单使用
- AVL树(Adelson-Velskii-Landis tree)
- P51 1
- Json转Java对象
- 八大排序算法