ZOJ 3829 Known Notation 贪心 难度:0
Known Notation
Time Limit: 2 Seconds Memory Limit: 65536 KB
Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathematics and computer science. It is also known as postfix notation since every operator in an expression follows all of its operands. Bob is a student in Marjar University. He is learning RPN recent days.
To clarify the syntax of RPN for those who haven't learnt it before, we will offer some examples here. For instance, to add 3 and 4, one would write "3 4 +" rather than "3 + 4". If there are multiple operations, the operator is given immediately after its second operand. The arithmetic expression written "3 - 4 + 5" in conventional notation would be written "3 4 - 5 +" in RPN: 4 is first subtracted from 3, and then 5 added to it. Another infix expression "5 + ((1 + 2) × 4) - 3" can be written down like this in RPN: "5 1 2 + 4 × + 3 -". An advantage of RPN is that it obviates the need for parentheses that are required by infix.
In this problem, we will use the asterisk "*" as the only operator and digits from "1" to "9" (without "0") as components of operands.
You are given an expression in reverse Polish notation. Unfortunately, all space characters are missing. That means the expression are concatenated into several long numeric sequence which are separated by asterisks. So you cannot distinguish the numbers from the given string.
You task is to check whether the given string can represent a valid RPN expression. If the given string cannot represent any valid RPN, please find out the minimal number of operations to make it valid. There are two types of operation to adjust the given string:
- Insert. You can insert a non-zero digit or an asterisk anywhere. For example, if you insert a "1" at the beginning of "2*3*4", the string becomes "12*3*4".
- Swap. You can swap any two characters in the string. For example, if you swap the last two characters of "12*3*4", the string becomes "12*34*".
The strings "2*3*4" and "12*3*4" cannot represent any valid RPN, but the string "12*34*" can represent a valid RPN which is "1 2 * 34 *".
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
There is a non-empty string consists of asterisks and non-zero digits. The length of the string will not exceed 1000.
Output
For each test case, output the minimal number of operations to make the given string able to represent a valid RPN.
Sample Input
3
1*1
11*234**
*
Sample Output
1
0
2
一开始的时候若不够lenofstar+1,放置lenofstar+1-lenofnumber个数字在前面,然后对不满足formerlenofnumber==formerlenofstar+1的星号都调到最后面和数字交换就行,注意最后一个如果是数字再和星号换回来
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char maz[2000];
int len,lenstar,addnum,addstar,lennum;
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%s",maz);
len=strlen(maz);addstar=lenstar=addnum=lennum=0;
for(int i=0;i<len;i++){
if(maz[i]=='*')lenstar++;
else lennum++;
}
if(lenstar==0){
puts("0");
continue;
}
int op=0;
if(lennum<lenstar+1){
addnum=lenstar+1-lennum;
}
op+=addnum;
int bakpnt=len;
for(int i=0;i<len;i++){
if(maz[i]=='*'){
if(addnum<addstar+2){
bakpnt--;
while(maz[bakpnt]=='*'){
bakpnt--;
}
swap(maz[bakpnt],maz[i]);
op++;
}
}
if(maz[i]=='*')addstar++;
else addnum++;
}
if(maz[len-1]!='*')op++;
printf("%d\n",op);
}
return 0;
}
ZOJ 3829 Known Notation 贪心 难度:0的更多相关文章
- ZOJ 3829 Known Notation 贪心
Known Notation Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showPro ...
- ZOJ 3829 Known Notation --贪心+找规律
题意:给出一个字符串,有两种操作: 1.插入一个数字 2.交换两个字符 问最少多少步可以把该字符串变为一个后缀表达式(操作符只有*). 解法:仔细观察,发现如果数字够的话根本不用插入,数字够的最 ...
- 贪心+模拟 ZOJ 3829 Known Notation
题目传送门 /* 题意:一串字符串,问要最少操作数使得成为合法的后缀表达式 贪心+模拟:数字个数 >= *个数+1 所以若数字少了先补上在前面,然后把不合法的*和最后的数字交换,记录次数 岛娘的 ...
- zoj 3829 Known Notation
作者:jostree 转载请说明出处 http://www.cnblogs.com/jostree/p/4020792.html 题目链接: zoj 3829 Known Notation 使用贪心+ ...
- 【贪心+一点小思路】Zoj - 3829 Known Notation
借用别人一句话,还以为是个高贵的dp... ... 一打眼一看是波兰式的题,有点懵还以为要用后缀表达式或者dp以下什么什么的,比赛后半阶段才开始仔细研究这题发现贪心就能搞,奈何读错题了!!交换的时候可 ...
- ZOJ - 3829 Known Notation(模拟+贪心)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 给定一个字符串(只包含数字和星号)可以在字符串的任意位置添加一个数字 ...
- ZOJ 3829 Known Notation(贪心)题解
题意:给一串字符,问你最少几步能变成后缀表达式.后缀表达式定义为,1 * 1 = 1 1 *,题目所给出的字串不带空格.你可以进行两种操作:加数字,交换任意两个字符. 思路:(不)显然,最终结果数字比 ...
- Known Notation ZOJ - 3829 (后缀表达式,贪心)
大意:给定后缀表达式, 每次操作可以添加一个字符, 可以交换两个字符的位置, 相邻数字可以看做一个整体也可以分开看, 求合法所需最少操作数. 数字个数一定为星号个数+1, 添加星号一定不会更优. 先判 ...
- ZOJ 3829 Known Notation (2014牡丹江H称号)
主题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=5383 Known Notation Time Limit: 2 S ...
随机推荐
- PN结讲解
可能大家在使用半导体器件的时候只是在使用它的电气属性,并没有很好的关心下它是什么原因才有了这样的电气属性,那么我们本篇就从物理结构分析下PN结吧. 首先看一张比较陈旧的图图: (就按自己的笔记简单谈谈 ...
- Pycharm 2017 激活码
Pycharm 2017 激活码 server选项里边输入: http://idea.liyang.io 我是通过这个成功的
- 【第四章】 springboot + swagger
注:本文参考自 http://www.jianshu.com/p/0465a2b837d2 swagger用于定义API文档. 好处: 前后端分离开发 API文档非常明确 测试的时候不需要再使用URL ...
- json封装数据,然后通过创造的标签调用
<ul class="list"> <li></li> <li></li> <li></li> ...
- caffe深度学习网络(.prototxt)在线可视化工具:Netscope Editor
http://ethereon.github.io/netscope/#/editor 网址:http://ethereon.github.io/netscope/#/editor 将.prototx ...
- 【TCP/IP详解 卷一:协议】第十章 动态选路协议
更为详细的RIP博客解析: RIP理论 距离向量算法的简介: RIP协议V-D算法的介绍 10.1 引言 静态选路修改路由表的三种方法 (1)主机设置时,默认的路由表项 (2)ICMP重定向报文(默认 ...
- MVC ---- T4(1)
T4 模板编辑插件:tangibleT4EditorPlusModellingToolsVS2012.msi 下载地址:http://t4-editor.tangible-engineering.co ...
- Vhost.conf 范例
NameVirtualHost *:80 <VirtualHost *:80> ServerName haofei.com DocumentRoot "E:/test/" ...
- Linux-Ubuntu14.04下mongodb安装部署
mongo 下载:https://www.mongodb.com/download-center/community?jmp=nav 创建目录,将下载的文件放在如下位置 xxx@ubuntu:/usr ...
- shell 算术运算符
算术运算符 下表列出了常用的算术运算符,假定变量 a 为 10,变量 b 为 20: 运算符 说明 举例 + 加法 `expr $a + $b` 结果为 30. - 减法 `expr $a - $b` ...