【52.49%】【codeforces 556A】Case of the Zeros and Ones
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
Examples
input
4
1100
output
0
input
5
01010
output
1
input
8
11101111
output
6
Note
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: .
【题目链接】:http://codeforces.com/contest/556/problem/A
【题解】
让你消除相邻的01子串;
因为看到挺多人过的,就用函数写了个暴力;
然后就过了。
不过…998ms…….
用数组模拟链表应该更快.
不过挺麻烦的吧.
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
#define pri(x) printf("%d",x)
#define prl(x) printf("%I64d",x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
const int MAXN = 2e5+100;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
string s;
int n;
int main()
{
//freopen("F:\\rush.txt","r",stdin);
cin>>n;
cin >> s;
s=' '+s;
int len = s.size();
len--;
int i = 1;
while (i <= len-1)
{
if (s[i]!=s[i+1])
{
s.erase(i,2);
i--;
if (i==0) i=1;
}
else
i++;
len = s.size()-1;
}
cout << len;
return 0;
}
【52.49%】【codeforces 556A】Case of the Zeros and Ones的更多相关文章
- Codeforces 556A:Case of the Zeros and Ones
A. Case of the Zeros and Ones time limit per test 1 second memory limit per test 256 megabytes input ...
- JAVA 基础编程练习题49 【程序 49 子串出现的个数】
49 [程序 49 子串出现的个数] 题目:计算字符串中子串出现的次数 package cskaoyan; public class cskaoyan49 { public static void m ...
- 【 BowWow and the Timetable CodeForces - 1204A 】【思维】
题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...
- Codeforces 556 A Case of the Zeros and Ones
A. Case of the Zeros and Ones time limit per test 1 second memory limit per test 256 megabytes input ...
- 【30.49%】【codeforces 569A】Music
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【59.49%】【codeforces 554B】Ohana Cleans Up
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 776E】The Holmes Children
[题目链接]:http://codeforces.com/contest/776/problem/E [题意] f(n)是小于n的不同整数对(x,y)这里x+y==n且gcd(x,y)==1的个数; ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【搜索】【并查集】Codeforces 691D Swaps in Permutation
题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交 ...
随机推荐
- Prism 框架基础架构
概要 Prism提供指导,帮助您更轻松地设计和构建,灵活且易于维护的客户端业务应用程序,这些应用程序可在Windows运行时,Windows Presentation Foundation(WPF)桌 ...
- Spring 实现数据库读写分离(转)
现在大型的电子商务系统,在数据库层面大都采用读写分离技术,就是一个Master数据库,多个Slave数据库.Master库负责数据更新和实时数据查询,Slave库当然负责非实时数据查询.因为在实际的应 ...
- 对 hiren bootcd 15.2 中的 XP 系统作了汉化, 同时支持中文输入法。提供下载
对 hiren bootcd 15.2 中的 XP 系统作了汉化, 同时支持中文输入法.提供下载 对该PE 中的 XP 系统作了汉化, 由于一个 中文字库 就要 10M 多:加之原系统过于精简,对中文 ...
- 根据点画线java
package com.yang; import java.awt.Color; import java.awt.Graphics; import java.util.ArrayList; impor ...
- [转]Accept-Encoding
原文地址:https://blog.csdn.net/sqzhao/article/details/49499471 HTTP Header中Accept-Encoding 是浏览器发给服务器,声明浏 ...
- codeforces 204E. Little Elephant and Strings(广义后缀自动机,Parent树)
传送门在这里. 大意: 给一堆字符串,询问每个字符串有多少子串在所有字符串中出现K次以上. 解题思路: 这种子串问题一定要见后缀自动机Parent树Dfs序统计出现次数都是套路了吧. 这道题统计子串个 ...
- 【习题 7-9 UVA-1604】Cubic Eight-Puzzle
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] IDA* 保证这次移动的方格不和前一次重复. 然后加一个8数码的剪枝就行了. ->看看当前状态和目标状态有多少个地方是不一样的 ...
- textview-显示行数限制
在代码中直接添加 android:maxLines="2" android:ellipsize="end" 跟ellipsize搭配使用,超过两行的时候,第二行 ...
- 非极大值抑制(non-maximum suppression)的理解与实现
非极大抑制(Non-Maximum Suppression) Non-Maximum Suppression for Object Detection in Python RCNN 和微软提出的 SP ...
- ASP.Net中页面传值的几种方式
开篇概述 对于任何一个初学者来说,页面之间传值可谓是必经之路,却又是他们的难点.其实,对大部分高手来说,未必不是难点. 回想2016年面试的将近300人中,有实习生,有应届毕业生,有1-3年经验的,有 ...