CodeForces NO.556A Case of the Zeros and Ones(stack,110101010)
2016-02-17 19:55
387 查看
问题描述:
如果“1”与“0”或者“0”与“1”相遇会消掉。
题目链接:CodeForces NO.556A
没啥难度,用到了两种做法,直接上代码:
如果“1”与“0”或者“0”与“1”相遇会消掉。
题目链接:CodeForces NO.556A
没啥难度,用到了两种做法,直接上代码:
第一种: 技巧,遍历数组,计算“0”与“1”的个数,然后作差取绝对值即可。 #include<iostream> #include<cstdio> #include<cmath> #include<queue> #include<stack> #include<cstring> #include<algorithm> #define INF 0x3f3f3f3f #define MAX 200004 #define PI 3.1415926 using namespace std; int main(){ int n; char arr[MAX]; scanf("%d", &n); scanf("%s", arr); int c0 = 0, c1 = 0; for(int i = 0; i < n; i++){ if(arr[i] == '0') c0++; else c1++; } if(c0 < c1) swap(c0, c1); printf("%d\n", c0 - c1); return 0; }
第二种: 用到了栈 #include <cstdio> #include <algorithm> #include <iostream> #include <stack> using namespace std; #define MAX 200001 int main(){ int n; cin >> n; char arr[MAX]; stack<int> s; cin >> arr; int len = strlen(arr); for(int i = 0; i < len; i++){ if(s.empty()) s.push(arr[i]); else if(arr[i] != s.top()) s.pop(); else s.push(arr[i]); } int len1 = s.size(); cout << len1 << endl; return 0; }
相关文章推荐
- 笔记
- 2016 2 17 bzoj炸
- linux shell 模仿写的一个唱片记录系统
- 初学注解笔记
- 下载软件更新包更新软件
- hdu-1247
- 日期格式转换问题MappingException: Could not determine type for: java.sql.String
- Android 捕获全局异常信息并进行相关处理
- MLlearning(2)——simHash算法
- LeetCode Find the Duplicate Number(一次通过)
- FFmpeg 3.0 版发布
- SSIS ->> Environment Variables
- 拥抱高效、拥抱 Bugtags 之来自用户的声音(四)
- BestCoder Round #72 (div.2)
- Android运营商名称显示之PLMN的读取(原)
- 简单SQLite的使用
- UIBezierPath个人学习总结
- Mysql“集群”和”主从“两者的区别
- 这些年正Android - 母亲
- 06笨小猴(1.9)