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 ...
随机推荐
- python中用filter求素数
#用filter求素数 #生成器,生成一个无限序列 def _odd_iter(): n=1 while True: n=n+2 yield n #筛选函数 def _not_divisible(n) ...
- Linux命令(1) - 查看内存使用情况: free -hm
使用"free -hm"命令查看linux服务器的内存使用状况,其中-h表示人性化显示,-m表示将内存显示为M:
- Request 接收参数乱码原理解析
起因: 今天早上被同事问了一个问题:说接收到的参数是乱码,让我帮着解决一下. 实际情景: 同事负责的平台是Ext.js框架搭建的,web.config配置文件里配置了全局为“GB2312”编码: &l ...
- java 反射机制的实例
[案例1]通过一个对象获得完整的包名和类名 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package Reflect; /** * 通过一个对象获得完整的包名和类名 ...
- WebDriver 在使用 CSS Selector 与 XPath 在查找元素时如何取舍
开发在做Web系统时,用的是css div划分层,使用jQuery 选取元素.
- Java集合框架:HashMap
转载: Java集合框架:HashMap Java集合框架概述 Java集合框架无论是在工作.学习.面试中都会经常涉及到,相信各位也并不陌生,其强大也不用多说,博主最近翻阅java集合框架的源码以 ...
- css选择符有哪些?哪些属性可以继承?优先级算法如何计算?内联和important哪个优先
通配选择符* { sRules } 类型选择符E { sRules } td { font-size:14px; width:120px; } 属性选择符 E [ attr ] { sRule ...
- Core Data系列文章(一)Core Data基础
在iOS开发数据库SQLite的使用介绍了iOS中使用SQLite对数据进行持久化存储,实际上是对数据库直接进行操作,而苹果专门有一套API来间接的对数据进行持久化存储,而且主要针对用户创建的对象 - ...
- Java初学之华容道游戏
package hhuarongdao; public class example { public static void main(String args[]) { new Hua_Rong_Ro ...
- android:clipToPadding和android:clipChildren
假设我们要做一个效果,界面最顶部是一个ActionBar并且是半透明的,ActionBar下面是一个ListView,在初始状态 下, ListView是top是在ActionBar的bottom位置 ...