第五次比赛的水题
2015-07-31 18:56
316 查看
Description
Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones.
Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we choose any two adjacentpositions in the string, and if one them contains 0, and the other contains 1, then we are allowed to remove these two digits from the string, obtaining a string of length n - 2 as a result.
Now Andreid thinks about what is the minimum length of the string that can remain after applying the described operation several times (possibly, zero)? Help him to calculate this number.
Input
First line of the input contains a single integer n (1 ≤ n ≤ 2·105), the length of the string that Andreid has.
The second line contains the string of length n consisting only from zeros and ones.
Output
Output the minimum length of the string that may remain after applying the described operations several times.
Sample Input
Input
Output
Input
Output
Input
Output
Hint
In the first sample test it is possible to change the string like the following:
.1100到10再到空
In the second sample test it is possible to change the string like the following:
.01010到010到0
In the third sample test it is possible to change the string like the following:
.11101111到111111
题意:给一个字符串(注意是字符串),如果相邻的两个是0和1的话,就把他们拿去,一直这样最后输出字符串的长度,还有值得注意的就是这里只要求输出和输入一组数据......。
解题思路:开始看见题目,理解玩意思后,差点就被它唬住了。第一想到的是标记递归什么的,然后仔细一想,其实不用这么麻烦。字符串中只要有0和1,就一定会弹出去。这样就可以把问题转化为看这个字符串有多少个1和0,用多的减去少的,剩下的就是字符串长度.......
(注意这里要用字符串,我看见数字第一反应就是用数组,然后就一直没有输出。明明知道做却卡在了数组上半个小时多,这酸爽....
最后还是用了字符串,因为题目意思是用字符串,并且用数组不可以连续输入数字,例如输入1100,程序会将1100理解为一个数字.......
希望有人和我犯了一样的傻逼错误的时候可以改过来............(真是自己把自己蠢哭.....))
代码如下:
Andrewid the Android is a galaxy-famous detective. In his free time he likes to think about strings containing zeros and ones.
Once he thought about a string of length n consisting of zeroes and ones. Consider the following operation: we choose any two adjacentpositions in the string, and if one them contains 0, and the other contains 1, then we are allowed to remove these two digits from the string, obtaining a string of length n - 2 as a result.
Now Andreid thinks about what is the minimum length of the string that can remain after applying the described operation several times (possibly, zero)? Help him to calculate this number.
Input
First line of the input contains a single integer n (1 ≤ n ≤ 2·105), the length of the string that Andreid has.
The second line contains the string of length n consisting only from zeros and ones.
Output
Output the minimum length of the string that may remain after applying the described operations several times.
Sample Input
Input
4 1100
Output
0
Input
5 01010
Output
1
Input
8 11101111
Output
6
Hint
In the first sample test it is possible to change the string like the following:
.1100到10再到空
In the second sample test it is possible to change the string like the following:
.01010到010到0
In the third sample test it is possible to change the string like the following:
.11101111到111111
题意:给一个字符串(注意是字符串),如果相邻的两个是0和1的话,就把他们拿去,一直这样最后输出字符串的长度,还有值得注意的就是这里只要求输出和输入一组数据......。
解题思路:开始看见题目,理解玩意思后,差点就被它唬住了。第一想到的是标记递归什么的,然后仔细一想,其实不用这么麻烦。字符串中只要有0和1,就一定会弹出去。这样就可以把问题转化为看这个字符串有多少个1和0,用多的减去少的,剩下的就是字符串长度.......
(注意这里要用字符串,我看见数字第一反应就是用数组,然后就一直没有输出。明明知道做却卡在了数组上半个小时多,这酸爽....
最后还是用了字符串,因为题目意思是用字符串,并且用数组不可以连续输入数字,例如输入1100,程序会将1100理解为一个数字.......
希望有人和我犯了一样的傻逼错误的时候可以改过来............(真是自己把自己蠢哭.....))
代码如下:
#include <stdio.h> #include <string> char a[200010]; int main() { int n; scanf("%d",&n); int m1=0,m2=0; scanf("%s",&a); for(int j=0; j<n; j++) { if(a[j]=='0') m1=m1+1; if(a[j]=='1') m2=m2+1; } if(m1>=m2) printf("%d\n",m1-m2); else printf("%d\n",m2-m1); return 0; }
相关文章推荐
- 第二周习题F
- LVS详细介绍,配置,用法
- 集群LVS 实现数据共享
- linux日常管理
- 学习JAVA开发心得第一篇
- 关于iOS多线程
- 【一些事晚报】什么设计让你一整天爽歪歪?
- 趣味数组(一)
- [Android]可缩放性ImageView(可以放大缩小)
- 关于Talend的Patch分支对应Eclipse开发环境的配置总结.
- MFC实现的串口测试程序
- Yii 跨数据库关联关系
- 从Eclipse到Android Studio
- 精灵点点基础教程2 -- 基本操作
- git
- redis --初级笔记
- python中的getopt模块
- opencv简单滤波
- 湖南省第六届省赛有趣题
- HDU 2795 Billboard 线段树 顺序点更新