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 : ...
随机推荐
- mysql 登录修改密码与数据库备份
1.mysql首次登录修改密码 mysql -uroot -p(首次登录无密码,按回车即可:如果无法登陆,修改mysql配置文件,在[mysqld]最后一行加skip-grant-tables即可登录 ...
- Codeforces 1247E. Rock Is Push
传送门 显然考虑 $dp$ ,设 $fx[i][j]$ 表示从 $(i,j)$ 出发往下走一格,最终到达 $(n,m)$ 的方案数,$fy[i][j]$ 表示从 $(i,j)$ 出发往右走一格,最终到 ...
- Asp.net core 学习笔记 ef core Surrogate Key, Natural Key, Alternate Keys
更新: 2019-12-23 foreignkey 并不一样要配上 alternate key,其实只要是 unique 就可以了. 和 sql server 是一样的, 经常有一种错觉 primar ...
- SMTP实现发送邮箱2(封装版)
SMTP.h #ifndef __SMTP_H__ //避免重复包含 #define __SMTP_H__ #include <iostream> #include <list> ...
- Servlet获取JSP中的汉字乱码问题解决方案
1.String customerName=request.getParameter("customer_name");这样会出现乱码 解决方案很简单: String custom ...
- Spark面试知识点-SparkSQL(1)
0.介绍: (1)Spark SQL的前身是Shark,即Hive on Spark, 1.SparkSQL特点: (1)支持多种数据源:Hive,RDD,Parquet,JSON,JDBC等. (2 ...
- input type=color 设置颜色
在设置背景色的时候,使用html5 type=color 标签,但是初始值一直都是黑色的,背景如果没有设置的时候,应该是白色,比如文本图元,所以需要设置一个初始的颜色值, 注意: value不实用,怎 ...
- js-回文数
回文数 设n是一任意自然数.若将n的各位数字反向排列所得自然数n1与n相等,则称n为一回文数. //回文数 let readline = require("readline-sync&quo ...
- 【Day4】1.JsonPath使用案例
import json python_data = [ { 'username': 'normal', 'vip': True, }, { 'username':None, 'vip':False } ...
- zip命令分卷压缩和解压缩
创建分卷压缩文件 zip -s 100m -r folder.zip folder/ -s: 创建分卷的大小 -r: 循环压缩文件夹下面的内容 切分已有的文件: zip existing.zip -- ...