A. Case of the Zeros and Ones(Codeforces Round #310 (Div. 2) 栈)
来源:互联网 发布:淘宝达人赚钱 编辑:程序博客网 时间:2024/06/05 03:28
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.
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 the minimum length of the string that may remain after applying the described operations several times.
41100
0
501010
1
811101111
6
In the first sample test it is possible to change the string like the following: .
In the second sample test it is possible to change the string like the following: .
In the third sample test it is possible to change the string like the following: .
题意:有一个n个数字的序列,如果相邻的两个数字是10或者01就可以消除,消
除之后由原来不相邻变为相邻的两个数字符合这种情况同样可以消除,问消除之后这
个序列还剩几个数字
思路:栈正好就好符合这种特性;
#include<iostream>#include<algorithm>#include<stdio.h>#include<string.h>#include<stdlib.h>#include<stack>using namespace std;int n;char str[200010];stack<char>q;int main() { while(scanf("%d",&n)!=EOF) { scanf("%s",str); while(!q.empty()) { q.pop(); } for(int i=0; str[i]!='\0'; i++) { if(q.empty()) { q.push(str[i]); } else { if((q.top() == '1' && str[i] == '0') || (q.top() == '0' && str[i] == '1')) { q.pop(); } else { q.push(str[i]); } } } printf("%d\n",q.size()); } return 0;}
- A. Case of the Zeros and Ones(Codeforces Round #310 (Div. 2) 栈)
- Codeforces Round #310 (Div. 2)-A. Case of the Zeros and Ones
- CodeForces 556A Case of the Zeros and Ones
- CodeForces-556A Case of the Zeros and Ones
- codeforces 556A Case of the Zeros and Ones
- Codeforces 556A:Case of the Zeros and Ones
- CodeForces 556A Case of the Zeros and Ones
- 【52.49%】【codeforces 556A】Case of the Zeros and Ones
- A. Case of the Zeros and Ones
- A. Case of the Zeros and Ones
- CodeForces NO.556A Case of the Zeros and Ones(stack,110101010)
- Codeforces 556A Case of the Zeros and Ones 消除01串
- coderforce A. Case of the Zeros and Ones
- Case of the Zeros and Ones
- Case of the Zeros and Ones
- Case of the Zeros and Ones
- CF#310-A-Case of the Zeros and Ones-链表模拟
- the Zeros and Ones
- 移动平均数最简洁的算法
- format HDD 格式化硬盘 块设备 code
- RegularExpression(Regex)
- 将字符串插入到指定位置
- maven管理SpringMVC+Mybatis无法加载Mapper.xml
- A. Case of the Zeros and Ones(Codeforces Round #310 (Div. 2) 栈)
- android AudioManager类 详解
- oracle卸载
- iOS —— 设备信息获取
- 重装windows后修复grub2引导
- typedef
- 上班族英语用语:今天你加班吗?
- PPT(PowerPoint)基础使用技巧
- Flume案例