LA 6979 Known Notation 构造+贪心 铜牌题
6979 Known Notation
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:
1. 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”.
2. 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
1*1
11*234**
*ACM-ICPC Live Archive: 6979 – Known Notation 2/2
Sample Output
题意:给出一个字符串,有两种操作: 1.插入一个数字 2.交换两个字符 问最少多少步可以把该字符串变为一个后缀表达式(操作符只有*)
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <map>
#include <bitset>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set> #define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define CT continue
#define SC scanf char s[1005];
int main()
{
int cas;SC("%d",&cas);
while(cas--){
SC("%s",s);
int len=strlen(s),num=0,ln=0,star=0,ans=0;
for(int i=0;s[i]!='\0';i++)
if(s[i]=='*') star++;
else num++;
if(num<star+1) {ans+=star+1-num;ln=star+1-num;} for(int i=0;s[i]!='\0';i++){
if(s[i]>='0'&&s[i]<='9') ln++;
else{
if(ln>=2) ln-=1;
else {
ans++;
for(int j=len-1;j>=0;j--) if(s[j]!='*'){
swap(s[j],s[i]);ln++;break;
}
}
}
}
printf("%d\n",ans);
}
return 0;
}
错因分析:看错题目了,,以为只能相邻两个交换,结果想了两个多小时,,,Orz;
构造:
(1).首先,我们可以看到对于有k个*的话。至少k+1个数字才能保证合法,所以不够的话,需要添加
(2).然后可以构造出11111...**...这样在满足(1)的前提下必然不需要然和操作就能保证合法的式子。
贪心:考虑到当前额*,因为不合法,那么至少需要一个操作才能保证合法,由构造可知,将该*掉到
末尾的数字上去,不仅能保证再次遇到该*时不再浪费额外的操作,也能尽量的贪心,让该*之后的接下来的
*有更多的数字去抵消,尽可能的不需要交换。
LA 6979 Known Notation 构造+贪心 铜牌题的更多相关文章
- 贪心/构造/DP 杂题选做Ⅱ
由于换了台电脑,而我的贪心 & 构造能力依然很拉跨,所以决定再开一个坑( 前传: 贪心/构造/DP 杂题选做 u1s1 我预感还有Ⅲ(欸,这不是我在多项式Ⅱ中说过的原话吗) 24. P5912 ...
- 贪心/构造/DP 杂题选做Ⅲ
颓!颓!颓!(bushi 前传: 贪心/构造/DP 杂题选做 贪心/构造/DP 杂题选做Ⅱ 51. CF758E Broken Tree 讲个笑话,这道题是 11.3 模拟赛的 T2,模拟赛里那道题的 ...
- ZOJ Problem Set - 3829Known Notation(贪心)
ZOJ Problem Set - 3829Known Notation(贪心) 题目链接 题目大意:给你一个后缀表达式(仅仅有数字和符号),可是这个后缀表达式的空格不幸丢失,如今给你一个这种后缀表达 ...
- 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas
题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...
- 贪心/思维题 UVA 11292 The Dragon of Loowater
题目传送门 /* 题意:n个头,m个士兵,问能否砍掉n个头 贪心/思维题:两个数组升序排序,用最弱的士兵砍掉当前的头 */ #include <cstdio> #include <c ...
- 贪心/构造/DP 杂题选做
本博客将会收录一些贪心/构造的我认为较有价值的题目,这样可以有效的避免日后碰到 P7115 或者 P7915 这样的题就束手无策进而垫底的情况/dk 某些题目虽然跟贪心关系不大,但是在 CF 上有个 ...
- BZOJ 1124: [POI2008]枪战Maf(构造 + 贪心)
题意 有 \(n\) 个人,每个人手里有一把手枪.一开始所有人都选定一个人瞄准(有可能瞄准自己).然后他们按某个顺序开枪,且任意时刻只有一个人开枪. 因此,对于不同的开枪顺序,最后死的人也不同. 问最 ...
- 【每日一题】UVA - 1368 DNA Consensus String 字符串+贪心+阅读题
https://cn.vjudge.net/problem/UVA-1368 二维的hamming距离算法: For binary strings a and b the Hamming distan ...
- LightOJ 1166 Old Sorting 置换群 或 贪心 水题
LINK 题意:给出1~n数字的排列,求变为递增有序的最小交换次数 思路:水题.数据给的很小怎么搞都可以.由于坐标和数字都是1~n,所以我使用置换群求循环节个数和长度的方法. /** @Date : ...
随机推荐
- fiddler笔记:TimeLine时间轴选项卡
1.TimeLine选项卡介绍 TimeLine选项卡支持使用"瀑布"模型查看1~250个选中的Session.主要用于帮助性能分析和理解请求之间的关联.选项卡的主体内容是数据流视 ...
- 下载HTMLTestRunner 地址
通过pip安装 HTMLTestRunne失败 则需要通过手动下载. 下载地址: http://tungwaiyip.info/software/HTMLTestRunner.html 下载后,把H ...
- k8s之dashboard认证、资源需求、资源限制及HeapSter
1.部署dashboard kubernetes-dashboard运行时需要有sa账号提供权限 Dashboard官方地址:https://github.com/kubernetes/dashboa ...
- 2017多校赛 Function
Function Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- BIOS将MBR读入0x7C00地址处(x86平台下)
BIOS将MBR读入0x7C00地址处(x86平台下) https://www.cnblogs.com/jikebiancheng/p/6193953.html http://www.ruanyife ...
- react用高阶组件实现路由守卫
react-router不像vue-router一样有很多钩子函数,可以做路由守卫.想实现路由守卫,可以用高阶组件来实现. @connect(state => ({ isLogin: state ...
- XML模块与类的定义
xml模块 xml介绍: --XML 全称 可扩展标记语言 --<tag></tag> 双标签 标签中间可以加入文本信息 --<tag/>单标签 没有文本 ...
- vue组件常用传值
一.使用Props传递数据 在父组件中使用儿子组件 <template> <div> 父组件:{{mny}} <Son1 :mny="mny"&g ...
- Oracle---PL/SQL的学习
PL/SQL程序 一.定义 declare 说明部分 begin 语句序列(DML语句) exception 例外处理语句 end; 二. 变量和常量说明 a) 说明变量(char,varchar2, ...
- 四款免费好用的Bootstrap ui编辑器
Bootstrap带来了设计革命,本文介绍的四种免费Bootstrap在线设计工具,可视化所见所得设计网页,然后输出Html/CSS代码,其中有些甚至可以实现拖曳,也有可以设定自己的主题模板Theme ...