ZOJ Problem Set - 3829Known Notation(贪心)
ZOJ Problem Set - 3829Known Notation(贪心)
题目大意:给你一个后缀表达式(仅仅有数字和符号),可是这个后缀表达式的空格不幸丢失,如今给你一个这种后缀表达式,问最少须要多少操作能够把这个表达式变成合法的。
操作:
1、在这个表达式的不论什么位置插入‘’或者数字(一位数)。
2、把这个表达式的不论什么两个位置的字符对换。
解题思路:
一開始想的好复杂,结果还是漏了某种情况,一直过不去;就是卡在了碰到‘’的时候,数字不够是插入好还是替换好。
事实上仅仅要这么想:首先,数字的个数至少要是符号的个数 + 1.先求数字和符号的个数。数字不够插入自然更优,否则替换更好,而且插入数字在最越前面越好,替换‘’替换在越后面越好。
注意:所有是数字的情况。
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1005;
char str[maxn];
int main () {
int T;
scanf ("%d", &T);
while (T--) {
scanf ("%s", str);
int num, op, ans;
int len = strlen (str);
ans = num = op = 0;
for (int i = 0; i < len; i++)
if (str[i] == '*')
op++;
num = len - op;
if (!op) {
printf ("0\n");
continue;
}
if (str[len - 1] != '*') {
ans++;
for (int i = 0; i < len; i++)
if (str[i] == '*') {
swap(str[i], str[len - 1]);
break;
}
}
int cnt = 0;
for (int i = 0; i < len; i++) {
if (str[i] == '*') {
if (cnt > 1)
cnt--;
else {
if (num >= op + 1) {
for (int j = len - 1; j >= 0; j--)
if (str[j] != '*') {
swap(str[j], str[i]);
cnt++;
ans++;
break;
}
} else {
ans++;
num++;
if (!cnt) {
i--;
cnt = 1;
}
}
}
} else
cnt++;
}
printf ("%d\n", ans);
}
return 0;
}
ZOJ Problem Set - 3829Known Notation(贪心)的更多相关文章
- ZOJ Problem Set - 1394 Polar Explorer
这道题目还是简单的,但是自己WA了好几次,总结下: 1.对输入的总结,加上上次ZOJ Problem Set - 1334 Basically Speaking ac代码及总结这道题目的总结 题目要求 ...
- ZOJ Problem Set - 1025解题报告
ZOJ Problem Set - 1025 题目分类:基础题 原题地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=10 ...
- ZOJ Problem Set - 2563 Long Dominoes 【如压力dp】
称号:ZOJ Problem Set - 2563 Long Dominoes 题意:给出1*3的小矩形.求覆盖m*n的矩阵的最多的不同的方法数? 分析:有一道题目是1 * 2的.比較火.链接:这里 ...
- ZOJ Problem Set - 3593 拓展欧几里得 数学
ZOJ Problem Set - 3593 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3593 One Person ...
- ZOJ Problem Set - 2297 Survival 【状压dp】
题目:ZOJ Problem Set - 2297 Survival 题意:给出一些怪,有两个值,打他花费的血和能够添加的血,然后有一个boss,必须把小怪全部都打死之后才干打boss,血量小于0会死 ...
- ZOJ Problem Set - 3820 Building Fire Stations 【树的直径 + 操作 】
题目:problemId=5374" target="_blank">ZOJ Problem Set - 3820 Building Fire Stations 题 ...
- ZOJ Problem Set - 3229 Shoot the Bullet 【有上下界网络流+流量输出】
题目:problemId=3442" target="_blank">ZOJ Problem Set - 3229 Shoot the Bullet 分类:有源有汇 ...
- ZOJ Problem Set - 3822Domination(DP)
ZOJ Problem Set - 3822Domination(DP) problemCode=3822">题目链接 题目大意: 给你一个n * m的棋盘,每天都在棋盘上面放一颗棋子 ...
- ZOJ Problem Set - 3819Average Score
ZOJ Problem Set - 3819Average Score 题目链接 题目大意:给你两个班的的学生的分数(A,B班).A班有一个学生的分数没有给出. 如今要求你给出这个学生分数的上下限.使 ...
随机推荐
- android之写文件到sd卡
1.main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...
- Delphi中WebBrowser自动填表模板
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, ...
- 文件比较,文件夹比较-- vimdiff,beyond compare, compare suite, WinMerge,Kdiff3
文件比较,文件夹比较-- vimdiff,beyond compare, compare suite, WinMerge,Kdiff3 有一个项目的源码包需要比较,400M以上,这就要找个好的工具了 ...
- 修改Tabhost样式和字体大小和居中显示
有时候我们的tabhost并不需要贴图,所以这个时候就必须把文字居中显示和设置大小了,代码如下 setContentView(R.layout.home_vzo_tabhost); ...
- [Cocos2d-x]Android的android.mk文件通用版本
原文地址: http://blog.ready4go.com/blog/2013/10/12/update-android-dot-mk-with-local-src-files-and-local- ...
- 一致性哈希算法(consistent hashing)样例+測试。
一个简单的consistent hashing的样例,非常easy理解. 首先有一个设备类,定义了机器名和ip: public class Cache { public String name; pu ...
- Oracle Enterprise Manager Cloud Control 12c R4 安装配置
准备软件 em12.1.0.4_linux64_V45344-01.zip em12.1.0.4_linux64_V45345-01.zip em12.1.0.4_linux64_V45346-01. ...
- zoj2059(经典dp)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1059 分析:dp[i][j]表示前i个石头组成两座塔高度差为j的较低 ...
- Note for video Machine Learning and Data Mining——Linear Model
Here is the note for lecture three. the linear model Linear model is a basic and important model in ...
- Linux 单用户模式的使用
在进入系统启动菜单时,选择kernel,输入"e"后进入,在kernel开头的启动项后输入(空格) /single或者 / 1,然后输入"b"重新启动机器,此时 ...