Codeforces 556A Case of the Zeros and Ones 消除01串
来源:互联网 发布:全本网络下载下载 编辑:程序博客网 时间:2024/06/05 09:48
题意:给出一个只由0和1组成的串,每次操作可以选择两个相邻的位置且这两个位置一个为0,一个为1(顺序无所谓)并删去这两个位置得到一个新串,然后可以对新串继续操作。操作次数无限制,令原串的长度不断缩短直至不能再短。求这个最短的长度为多少。
虽然题目不难,不过还是证明一下自己思路的正确性。这里我主要是想说一下证明的思路。直观上看,只要发现两个相邻的是0和1就把它删去,不用考虑不同的删法会不会对后面造成影响。比如有如下串:.......101........ 删掉10和删掉01都无所谓。这里我们证明一个更为直接的结论:即无论怎么删,最后剩下的串的长度一定是固定的。
用反证法。假设存在两种不同的删法使得长度不一样,设第一种删法最后的长度为X,第二种删法最后的长度为Y。首先可以简单的想到,最后剩下来的串必定是只有一个数字组成,即要么全1要么全0(如果存在不同的一定还可以删)。我们假设原串长度为L。设第一种删法操作了a次,第二种删法操作了b次,因为每次操作减少2个长度,所以有
X + 2*a = L
Y + 2*b = L
而每次删的时候都是删去1个0和1,那么按照第一次删法,可求出原串中0和1的个数为X + a 、a。(不一定按顺序,下同),按照第二次删法,原串中0和1的个数为Y + b、b。显然对应的0和1的个数要相等。而a一定不等于b(因为X不等于Y)所以必有
a = Y + b
b = X + a
两式相加得到X + Y = 0,而X和Y都是非负整数,所以必有X = Y = 0,这显然与X不等于Y矛盾。所以最后剩下的串只有一种可能的长度。既然只有一种,那么无论怎么删都无所谓了。
我们用vector来实现。 遍历整个串,依次判断每个数,若容器空直接push,若容器尾部元素与当前数字不同,则pop,否则push。
#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <string>#include <algorithm>#include <stack>#include <queue>#include <vector>#include <map>#include <set>using namespace std;const int MAX = 200005;int n;char s[MAX];vector <char> v;void input(){ scanf("%s", s);}void solve(){ int lenth = strlen(s); v.clear(); for(int i = 0; i < lenth; i++) { if(v.size() == 0) v.push_back(s[i]); else { if(v[v.size() - 1] != s[i]) v.pop_back(); else v.push_back(s[i]); } } printf("%d\n", v.size());}int main(){ while(scanf("%d", &n) != EOF) { input(); solve(); } return 0;}
- Codeforces 556A Case of the Zeros and Ones 消除01串
- CodeForces 556A Case of the Zeros and Ones
- CodeForces-556A Case of the Zeros and Ones
- codeforces 556A Case of the Zeros and Ones
- Codeforces 556A:Case of the Zeros and Ones
- CodeForces 556A Case of the Zeros and Ones
- 【52.49%】【codeforces 556A】Case of the Zeros and Ones
- A. Case of the Zeros and Ones
- A. Case of the Zeros and Ones
- CodeForces NO.556A Case of the Zeros and Ones(stack,110101010)
- A. Case of the Zeros and Ones(Codeforces Round #310 (Div. 2) 栈)
- Codeforces Round #310 (Div. 2)-A. Case of the Zeros and Ones
- coderforce A. Case of the Zeros and Ones
- Case of the Zeros and Ones
- Case of the Zeros and Ones
- Case of the Zeros and Ones
- CF#310-A-Case of the Zeros and Ones-链表模拟
- the Zeros and Ones
- 孙悟空与白骨精情书
- Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead. Please use Android Tools
- Ubuntu 14.04+cuda7+caffe 安装配置指南
- 如何学习python
- C++ 之继承中的重载
- Codeforces 556A Case of the Zeros and Ones 消除01串
- cocos-js动态读取json
- oracle 用户创建
- 蒋宇捷——程序员的进化 - 在拉勾1024程序员节上的演讲
- IM即时通信
- CMFCToolTipCtrl 应用详解
- Ubuntu 下安装新世纪五笔输入法
- SQL中group by的用法
- OCP-1Z0-051-2015-23题