Problem A CodeForces 556A
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 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 Input
4
1100
0
5
01010
1
8
11101111
6 题目大意:给出0和1两个数组成的序列,0和1相邻就可以配对,输出剩下不能配对的个数。 思路:配对问题,首先就可以用栈来实现,由于序列可以很大,所以用字符串来存储最好,先将第一个字符入栈, 如果它后面一个字符和它不同则将它出栈,否则就入栈;不过这个题目还可以用更简单的方法,因为0和1配对没先后 顺序,所以只要分别标记有多少个0和1,再将它们的个数相减再取绝对值,最后将这个绝对值输出即可。
#include <iostream>
#include <cstdio>
#include <cstring>
const int maxn=200000;
char s[maxn];
using namespace std;
int main()
{
int n,c,flag,kase;
while(scanf("%d",&n)==1&&n)
{
c=0;
flag=0,kase=0;
scanf("%s",s);
c=strlen(s);
for(int i=0;i<c;i++)
{
if(s[i]-'0'==1)
++flag;
if(s[i]-'0'==0)
++kase;
}
if(flag>=kase)
printf("%d\n",flag-kase);
else
printf("%d\n",kase-flag);
}
return 0;
}
Problem A CodeForces 556A的更多相关文章
- Problem - D - Codeforces Fix a Tree
Problem - D - Codeforces Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作 ...
- Codeforces Round #439 (Div. 2) Problem E (Codeforces 869E) - 暴力 - 随机化 - 二维树状数组 - 差分
Adieu l'ami. Koyomi is helping Oshino, an acquaintance of his, to take care of an open space around ...
- Codeforces Round #439 (Div. 2) Problem C (Codeforces 869C) - 组合数学
— This is not playing but duty as allies of justice, Nii-chan! — Not allies but justice itself, Onii ...
- Codeforces Round #439 (Div. 2) Problem B (Codeforces 869B)
Even if the world is full of counterfeits, I still regard it as wonderful. Pile up herbs and incense ...
- Codeforces Round #439 (Div. 2) Problem A (Codeforces 869A) - 暴力
Rock... Paper! After Karen have found the deterministic winning (losing?) strategy for rock-paper-sc ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem F (Codeforces 831F) - 数论 - 暴力
题目传送门 传送门I 传送门II 传送门III 题目大意 求一个满足$d\sum_{i = 1}^{n} \left \lceil \frac{a_i}{d} \right \rceil - \sum ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案 - 动态规划
There are n people and k keys on a straight line. Every person wants to get to the office which is l ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 831E) - 线段树 - 树状数组
Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this int ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem C (Codeforces 831C) - 暴力 - 二分法
Polycarp watched TV-show where k jury members one by one rated a participant by adding him a certain ...
随机推荐
- Oracle一个用户查询另一个用户的表数据
1.两个用户是在不同的库,需要建立dblink 2.属于同一个库的不同用户 1)方法一:使用"用户名."的方式访问 例如:要从USER1账号访问USER2中的表TABLE2 A. ...
- mysql连接字符串
MySQL中 concat 函数使用方法:CONCAT(str1,str2,…)
- 论APP测试中黑盒测试方案的重要性?
运筹帷幄之中,决胜千里之外.古人足不出户,通过正确的部署就能决定千里之外战争的胜利!而于测试人员而言,制定正确的测试方案,就是日后测试就是是否顺利的决定性因素. 在整个测试过程中,对测试人员.资源以及 ...
- PHP获取某年第几周的开始日期和结束日期
http://blog.csdn.net/qq_27080247/article/details/50835956 /** * 获取某年第几周的开始日期和结束日期 * @param int $year ...
- libCEF总结01下载、编译、入门
目 录 第1章 下载 1 1.1 下载 1 1.2 合并 1 第2章 cmake 4 2.1 编译简介 4 2.2 下载cmake 4 2.3 运行cmake ...
- 串行通讯之.NET SerialPort异步写数据
目录 第1章说明 2 1 为什么需要异步写数据? 2 2 异步写数据的代码 2 3 源代码 4 第1章说明 1 为什么需要异步写数据? 如下图所示,以波特率300打开一个串口. ...
- spring boot学习
window10的环境下 使用apache-maven-3.3.9 到https://github.com/spring-projects/spring-boot 下载源码包,解压到c:\spring ...
- 【bzoj3625】【xsy1729】小朋友和二叉树
[bzoj3625]小朋友与二叉树 题意 我们的小朋友很喜欢计算机科学,而且尤其喜欢二叉树. 考虑一个含有n个互异正整数的序列c[1],c[2],...,c[n].如果一棵带点权的有根二叉树满足其所有 ...
- jquery函数
1.ready函数 当 DOM(文档对象模型) 已经加载,并且页面(包括图像)已经完全呈现时,会发生 ready 事件. $(document).ready(function (){alert('11 ...
- js二级导航
js写二级导航要点 1.ul li 2.js获取元素 3.setInterval(function(),time); 代码如下 <style type="text/css"& ...