CodeForces 556A Case of the Zeros and Ones
来源:互联网 发布:外国语大学网络教育 编辑:程序博客网 时间:2024/05/20 05:24
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 adjacent positions 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 test(s)
input
4
1100
output
0
input
5
01010
output
1
input
8
11101111
output
6
题解找出输入0和1的序列中所有10或者01短序列,并且删除。得到最后序列的长度
//代码实现#include<iostream>#include<cstdio>#include<cstring>#define MAX 200010using namespace std;char str [MAX];int main(){ long long t; long long cnout=0; scanf("%lld",&t); scanf("%s",str); for(int i=0;i<t;i++) { if(str[i]=='0') //找出序列中的0字符 cnout++;//对0的个数进行计数 } long long x,y,z; x=cnout;//0的个数 y=t-cnout;//1的个数 if(x==y)//如果0和1的个数相等,所以的都会匹配得到0 z=0; if(x<y)//如果不相等得到不想匹配的序列长度 z=t-2*x; else z=t-2*y; printf("%lld",z); return 0;}
- 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 556A Case of the Zeros and Ones 消除01串
- CodeForces NO.556A Case of the Zeros and Ones(stack,110101010)
- A. Case of the Zeros and Ones(Codeforces Round #310 (Div. 2) 栈)
- Codeforces Round #310 (Div. 2)-A. Case of the Zeros and Ones
- 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
- Zend Studio Debug调试开发环境配置
- 华为 OJ 句子逆序
- 第一个周五
- 自定义iOS7导航栏背景,标题和返回按钮文字颜色
- iphone的表情保存到数据库失败-- utf8 ——utf8mb4
- CodeForces 556A Case of the Zeros and Ones
- [UVA120]Stacks of Flapjacks[STL][构造]
- ZOJ - 3829 Known Notation
- Java 序列化
- 悼念512汶川大地震遇难同胞——老人是真饿了
- iOS视频网站
- UIImage存为本地文件,UIImage转换为NSData
- 使用XML资源文件来定义颜色
- windows多线程同步和互斥关系