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 : ...
随机推荐
- dp 状态压缩
之前我们在讨论的dp形式当中, 大多数是对整数的动态规划, 然而对于集合而言呢 ? 我们使用 DFS 吗, 看起来也可以, 但是加上dp记忆 数组的 动态规划效率更高: 那么进一步讨论, 我们如何表示 ...
- LeetCode. 计数质数
题目要求: 统计所有小于非负整数 n 的质数的数量. 示例i: 输入: 10 输出: 4 解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 . 代码: class Soluti ...
- 7.Linux查找目录下的所有文件中是否含有某个字符串
grep -rn "map" * 说明:-r 是递归查找-n 是显示行号* : 表示当前目录所有文件,也可以是某个文件名
- java包装类的缓存机制(转)
出处: java包装类的缓存机制 java 包装类的缓存机制,是在Java 5中引入的一个有助于节省内存.提高性能的功能,只有在自动装箱时有效 Integer包装类 举个栗子: Integer a = ...
- Educational Codeforces Round 71 (Rated for Div. 2) Solution
A. There Are Two Types Of Burgers 题意: 给一些面包,鸡肉,牛肉,你可以做成鸡肉汉堡或者牛肉汉堡并卖掉 一个鸡肉汉堡需要两个面包和一个鸡肉,牛肉汉堡需要两个面包和一个 ...
- js 怎样判断用户是否在浏览当前页面
有些时候我们需要在项目中判断用户是否在浏览当前页面,或者当前页面是否处于激活状态.然后再进行相关的操作.浏览器中可通过window对象的onblur.onfocus判断,或者document的hidd ...
- virtual和override
偶然间看到的题,借此记录. class Program { static void Main(string[] args) { D d = new D(); //第一个D是申明类,第二个D是实例类 A ...
- c++11 原生字符串字面值
c++11 原生字符串字面值 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #in ...
- Vue中断axios请求-切换页面+重复请求
切换页面时中断 一.概述 在Vue单页面开发过程中,遇到这样的情况,当我切换页面时,由于上一页面请求执行时间长,切换到该页面时,还未执行完,这时那个请求仍会继续执行直到请求结束,此时将会影响页面性能, ...
- ReactNative 踩坑笔记
1.fatal error: 'React/RCTBridgeDelegate.h' file not found 这个问题是应为没有安装cocoaPods 所以无法安装ios需要的第三方库.安装co ...