zoj 3829 Known Notation(贪心)
来源:互联网 发布:linux dd命令详解 编辑:程序博客网 时间:2024/05/17 23:01
题目链接:zoj 3829 Known Notation
题目大意:给定一个不完整的后缀表达式,要求有2种不同操作,用尽量少的操作使得表达式完整。
解题思路:贪心,数字的个数要要保证比∗的个数多1,不够的话优先补在开头是最优的。然后遍历一遍字符串,碰到数字+1,碰到∗-1,保证数字的个数大于等1,如果不够减的话,可以和最后面的一个数字交换位置(用栈维护十分方便),因为添加和交换代价都是1。
不过这题数据实在够弱的,因为11∗1这种情况,至少需要添加一个∗号才可以(特判即可),但是居然也可以过,同步赛的时候因为考虑了这个还WA了一次,因为11的情况还是为0的。
#include <cstdio>#include <cstring>#include <stack>#include <algorithm>using namespace std;const int maxn = 1005;char s[maxn];int solve () { stack<int> sta; int len = strlen(s), c = 0; for (int i = 0; i < len; i++) { if (s[i] == '*') c++; else sta.push(i); } if (c == 0) return 0; int n = len - c; int ret = n = max(c + 1 - n, 0); for (int i = 0; i < len; i++) { if (s[i] == '*') { if (n <= 1) { s[sta.top()] = '*'; sta.pop(); n++; ret++; } else n--; } else n++; } if (ret == 0 && s[len-1] != '*') ret++; return ret;}int main () { int cas; scanf("%d", &cas); while (cas--) { scanf("%s", s); printf("%d\n", solve()); } return 0;}
3 0
- zoj 3829 Known Notation(贪心)
- zoj 3829 Known Notation 贪心
- ZOJ 3829 Known Notation 贪心
- zoj 3829 Known Notation (贪心)
- [贪心+模拟] zoj 3829 Known Notation
- ZOJ 3829 Known Notation --贪心+找规律
- ZOJ 3829 Known Notation(贪心)
- ZOJ 3829 Known Notation【贪心】【好题】
- ZOJ - 3829 Known Notation
- ZOJ 3829 Known Notation
- zoj 3829 Known Notation
- zoj 3829 Known Notation
- ZOJ - 3829 Known Notation
- ZOJ 3829 Known Notation
- ZOJ-3829-Known Notation
- ZOJ 3829 Known Notation
- Zoj 3829 Known Notation
- ZOJ Problem Set - 3829Known Notation(贪心)
- Java泛型解析(02):通配符限定
- Bug修复
- C++ 虚函数表解析
- 子网掩码
- Jbpm4.4+hibernate3.5.4+spring3.0.4+struts2.1.8 整合例子(附完整的请假流程例子)。
- zoj 3829 Known Notation(贪心)
- 什么是时间戳
- Jquery 中 ajaxSubmit使用讲解
- 安卓Android-X86 4.4安装教程 电脑安装安卓Android-X86教程
- Postgresql允许远程访问配置修改
- tabbar的使用
- 数据库连接报错之IO异常(The Network Adapter could not establish the connection)
- 接口的使用注意问题
- Class.forName和ClassLoader.loadClass区别