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

Input

41100

Output

0

Input

501010

Output

1

Input

811101111

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

我的代码

#include<iostream>
#include<cstdio>
#include<stack>

using namespace std;
stack<char> s;

int main()
{

    int n;
    cin >> n;
    getchar();
    char ch,temp;
    while (n--)
    {
        scanf("%c", &ch);
        //cout << ch << endl;
        if (s.empty())
            s.push(ch);
        else
        {
            temp = s.top();
            if (temp == ch)
                s.push(ch);
            else
            {
                s.pop();
            }
        }
    }
    cout << s.size() << endl;

    return 0;
}

时间: 2024-12-27 04:21:19

CodeForces 556A 解题心得的相关文章

codeforces#254DIV2解题报告

今天简直大爆发啊...吃了顿烧烤居然这么管事.....本弱渣居然做出来了3道,而且B题是我第一次在CF中用到算法..(以前最多也就是贪心...). 题目地址:codeforces#225 A题: 水题..不解释..5分钟1Y. 代码如下: #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> #include

第四章学习小结 串的模式匹配 解题心得体会

串的模式匹配 解题心得体会 关于串,模式匹配是其一个很重要的问题.针对这个问题,书上讲了两种模式匹配的算法,即BF算法和KMP算法,下面针对这两种算法的实现谈谈我的心得. 一.BF算法的探索 [错误代码1] #include<iostream> #include<string.h> using namespace std; typedef struct{ char ch[1000002]; int length; }SString; void Index_BF(SString S,

Codeforces Round #315 (Div. 2) A. Music 解题心得

原题: Description Little Lesha loves listening to music via his smartphone. But the smartphone doesn't have much memory, so Lesha listens to his favorite songs in a well-known social network InTalk. Unfortunately, internet is not that fast in the city

CodeForces 18C - Stripe 解题心得

原题: Description Once Bob took a paper stripe of n squares (the height of the stripe is 1 square). In each square he wrote an integer number, possibly negative. He became interested in how many ways exist to cut this stripe into two pieces so that the

Codeforces Round 273 Random Teams 解题心得

原题: Description n participants of the competition were split into m teams in some manner so that each team has at least one participant. After the competition each pair of participants from the same team became friends. Your task is to write a progra

Codeforces Round #313 (Div. 2) D. Equivalent Strings 解题心得

原题: Description Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are called equivalent in one of the two cases: They are equal. If we split string a into two halves of the sam

括号配对问题——解题心得

Description You are given a string consisting of parentheses () and []. A string of this type is said to be correct: (a)if it is the empty string (b)if A and B are correct, AB is correct, (c)if A is correct, (A ) and [A ] is correct. Write a program

wechall.net/stegano 解题心得

最近迷上了 www.wechall.net 网站,里面都是些与计算机相关的题目挑战.题目又分很多类型,例如:加密与解密.隐写术.网络攻防.趣味编程.数学逻辑等.题目有的简单,有的很难,需要一些知识和技巧.与其他题目挑战的网站不同的是,在其他类似性质的网站注册的用户可以绑定到 WeChall 网站,然后 WeChall 提供排名信息,而且也分得很细,什么按总分全球排名.什么在自己国家的排名.什么解答某种语言网站题目的排名等.可以从解题的人数判断题目的难易程度,有兴趣的朋友可以去注册,解题中也能学到

Ducci Sequence 解题心得

原题贴上 A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, ... , an), the next n-tuple in the sequence is formed by taking the absolute differences of neighboring integers: ( a1, a2, ... , an)  (| a1 - a2|,| a2