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 : ...
随机推荐
- Pots(POJ-3414)【BFS】
题意:有两个有着固定容量的茶壶,初始时都为空,要求用FILL,POUR,DROP三种操作来准确地得到C值,输出最少次数及操作方案. 思路:比赛的时候真是脑子不好使,根本没想到是搜索,看了别人的题解用搜 ...
- 认识函数(python)
一般的函数都是有参数的,函数的参数都是放在函数定义的括号里的,函数参数的命名规则和我们说的变量的命名规则基本一样,一定要清晰明了.(能概括出它的意义,让人阅读你的代码,就知道这个参数是干嘛的就行).当 ...
- Class.getResources()和classLoader.getResources()区别
Class.getResource(String path) path不以’/'开头时,默认是从此类所在的包下取资源: path 以’/'开头时,则是从ClassPath根下获取: package t ...
- 怎样限制第三方Cookie
使用Cookie的 SameSite 属性. 1. SameSite=Strict; 这个模式下, 服务器将会完全禁止第三方Cookie, 在跨站点时, 任何情况下都不会发送Cookie, 也就是说, ...
- 基于APM实现RPC服务和消息队列的指定消费
本文内容是基于公司现有框架整理的一篇专利文章.该框架包含完整的一套DevOps流程,包括工单系统(容器申请.服务部署等)\配置中心\路由配置中心\服务治理平台\消息治理平台\葛朗台(基于Docker+ ...
- 10-MySQlL DBA笔记-基础知识
第四部分 运维篇 首先来了解一下数据库的定义,数据库是高效的.可靠的.易用的.安全的多用户存储引擎,我们可以通过它访问大量的持久化数据.我们管理和维护数据库,本质上也是要确保如上的特性,尽可能地保证数 ...
- C语言如何才能使用bool类型
解决办法:加上头文件#include<stdbool.h> ,就可以直接使用类型bool = true/false; #include<stdio.h> 2 #include& ...
- Oracle 以及 PLSQL安装
今天重装系统遇到oracle 安装的问题咯 ,oracle安装过程中很多疑难杂症咯 1如果之前装过,记得去删除注册表的Oracle 相关的文件 ,请百度有很多教程咯 2这个必须要勾选的!因为的是11g ...
- java web开发跨域问题
分布式环境,前后端分离背景下跨域问题 1.1 设置页面document.domain去把2个页面之间的跨域交互统一 一级域名相同的情况下 调用者和页面提供者进行一个协调 页面提供者要在document ...
- 少勇 #import和@class的区别
#import与@class的区别1.import会包含这个类的所有信息,包括实体变量和方法,而@class只是告诉编译器,其后面声明的名称是类的名称,至于这些类是如何定义的,暂时不用考虑,后面会再告 ...