博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
A. Case of the Zeros and Ones----解题报告
阅读量:6720 次
发布时间:2019-06-25

本文共 2138 字,大约阅读时间需要 7 分钟。

A. Case of the Zeros and Ones

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

 
Input
4 1100
Output
0
Input
5 01010
Output
1
Input
8 11101111
Output
6

Sample Output

 

Hint

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: .

 

题目大意:

有一个只由0和1组成的字符串,每次可以任意选择相邻的两个字符且这两个字符一个为是0,一个是1(顺序无所谓)并去掉这两个位置得到一个新的字符串,然后可以对新串继续操作,直到不能继续操作为止。求这个最短的长度为多少。

 

分析:

观察输入输出后,发现其实就是计算0,1的个数,然后用大的减小的,输出不能配对的总个数。

代码:

 

1 #include
2 #include
3 using namespace std; 4 5 const int maxn=200001; 6 7 int main() 8 { 9 int n,i;10 scanf("%d",&n);11 char a[maxn];12 scanf("%s",a);13 int m0=0,m1=0;14 for( i=0;i<=n;i++)15 { 16 if(a[i]=='0') 17 m0++;18 if(a[i]=='1')19 m1++;20 }21 int b=m1-m0;22 int c=m0-m1;23 if(m0<=m1)24 printf("%d\n",b);25 else26 printf("%d\n",c); 27 return 0;28 }
View Code

由于我输入的是字符型,判断0,1时要带引号,这要注意。

 

 

 

 

转载于:https://www.cnblogs.com/ttmj865/p/4693186.html

你可能感兴趣的文章
Golang 内存管理源码剖析
查看>>
简单了解负载均衡
查看>>
github 提交 常见操作和常见错误
查看>>
Ubuntu安装Mysql
查看>>
10.01-火狐浏览器设置
查看>>
20.22 告警系统监控项目
查看>>
开源ITIL管理工具OTRS简单介绍
查看>>
spring+httpclient完美集成,封装常用客户端工具类
查看>>
11月15日云栖精选夜读:分布式服务框架Dubbo疯狂更新!阿里开源要搞大事情?...
查看>>
paho.mqtt.android代码逐步分析(三)
查看>>
Java基础——类和对象
查看>>
继承与派生
查看>>
WinServer2008 下IIS安装
查看>>
如何给Docker hub用户上传头像
查看>>
Docker入门系列之一:在一个Docker容器里运行指定的web应用
查看>>
健康链(HDC):基础公链为经,医疗引擎为纬
查看>>
中国《南方画刊》第2期
查看>>
以太坊智能合约示例
查看>>
区块链是什么?彻底理解只要150行java代码!
查看>>
使用MaxCompute Java SDK 执行任务卡住了,怎么办?
查看>>