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 ...
随机推荐
- [Hibernate] - EAGER and LAZY
Hibernate中的字段映射中的Fetch有两种方式:EAGER和LAZY Eager:全部抓取 Lazy:延迟抓取 如果在字段中声明为Eager,那么在取得当前Bean时,同时会抓取Bean中的关 ...
- Android_用户界面概述和数据单位
一.UI界面概述 UI,对于一个应用而言用户界面是非常重要的一部分,是应用的脸,用户对应用第一个印象来自于界面,因此如果没有完美的用户界面,很难留住用户. 好的用户界面会极大提高用户的使用欲望并维护客 ...
- ubuntu14.04-rocketmq单机搭建
需要环境: jdk(1.6+) git(如果clone源码,需要git,没有git直接下载gar包也行) maven3.x在安装之前确定自己已经安装了jdk:java -version 先获取reck ...
- quartz定时任务框架的使用
quartz定时任务时间设置 这些星号由左到右按顺序代表 : * * * * * * * ...
- [mondrian] 快速入门
一.下载地址 mondrian各个版本和工具等的下载地址: http://sourceforge.net/projects/mondrian/files/?source=navbar 注意:mondr ...
- C/C++源代码到可执行程序的过程详解
编译,编译程序读取源程序(字符流),对之进行词法和语法的分析,将高级语言指令转换为功能等效的汇编代码,再由汇编程序转换为机器语言,并且按照操作系统对可执行文件格式的要求链接生成可执行程序. 源代码-- ...
- JSON 换行、JSON \r\n、怎么处理 ?(转载)
参考地址: http://www.cnblogs.com/mamingbo/archive/2010/11/27/1889583.html 在最后json的字符串上:.Replace("\r ...
- DataTable 中Distinct操作
DataTable dt = ds.Tables[]; DataView dataView = dt.DefaultView; DataTable dtDistinct = dataView.ToTa ...
- 让ie678支持css一些属性及html标签
昨天写的一个页面,用的css3及html5的一些样式与标签,在ie8下看是没有效果的,然后就在晚上查找了一下如何能让ie8也能实现这些效果. 1.添加respond.js文件,Respond.js让I ...
- http请求详解
GET GET方法意思是获取被请求URI(Request-URI)指定的信息(以实体的格式).如果请求URI涉及到一个数据生成过程,那么这个过程生成的数据应该被作为实体在响应中返回而不是过程的源文本, ...