• A. Case of the Zeros and Ones解题报告


    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

    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<cstdio>
     2 #include<iostream>
     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     else
    26         printf("%d\n",c);    
    27     return 0;
    28 }
    View Code

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

     

     

     

     

  • 相关阅读:
    Spring data jpa使用枚举
    IO
    Tomcat相关问题
    flex中为控件添加监听器并计算
    导出excel
    webService常见问题
    从指定的路径中查找含有特殊字符串的文件
    flex中日期的格式化
    flex与后台及页面间对象的传递
    打印时有选择的打印
  • 原文地址:https://www.cnblogs.com/ttmj865/p/4693186.html
Copyright © 2020-2023  润新知