ACM学习历程——ZOJ 3829 Known Notation (2014牡丹江区域赛K题)(策略,栈)
Description
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 这个题目要能想到匹配的策略,其实还是不是特别难的。
对于这个题目,首先要确定的前提就是数字的个数一定要大于运算符的个数,因为这是二元运算,故两个数字和一个运算符能生成一个数字。其次,这种运算法,是具备结合律的,所以,对于多个数字对应一个运算符的情况,可以就近选择一定数量的数字和运算符进行运算。
这种情况下,当运算符数目大于等于数字数目时,可以优先考虑插入数的操作,让插入的数全部插在最前面,因为结合律的缘故,这种情况是最优的。其次,只需要每个运算符前都满足:到当前为止,运算符的个数始终小于数字个数,否则,将排在当前最后一个的数字与当前运算符交换(由于结合律的关系,考虑最后一个数字)。
最后,还要考虑两个问题:
1、一开始就是纯数字的问题,这个答案应该为0;
2、执行完所有的操作,最后一个字符不是*的,答案应该再加1. 代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <queue>
#define inf 0x3fffffff
#define esp 1e-10 using namespace std; int numc, numi, len;
char s[];
int Stack[], top; void Input ()
{
numc = ;
numi = ;
len = ;
top = ;
char ch;
for (;;)
{
ch = getchar();
if (ch == '\n')
break;
if (ch == '*')
numc++;
else
{
numi++;
Stack[top++] = len;
}
s[len++] = ch;
}
} int qt()
{
if (numc == )
return ;
int ans = ;
if (numi > numc)
numi = numc = ;
else
{
ans = numi = numc - numi + ;
numc = ;
}
for (int i = ; i < len; ++i)
{
if (s[i] == '*')
numc++;
else
numi++;
if (numc >= numi)
{
swap (s[i], s[Stack[top-]]);
top--;
ans++;
numc--;
numi++;
}
}
if (s[len-] != '*')
ans++;
return ans;
} int main()
{
//freopen ("test.txt", "r", stdin);
int T;
scanf ("%d", &T);
getchar();
for (int times = ; times < T; ++times)
{
Input();
printf ("%d\n", qt());
}
return ;
}
ACM学习历程——ZOJ 3829 Known Notation (2014牡丹江区域赛K题)(策略,栈)的更多相关文章
- ACM学习历程——ZOJ 3822 Domination (2014牡丹江区域赛 D题)(概率,数学递推)
Description Edward is the headmaster of Marjar University. He is enthusiastic about chess and often ...
- ZOJ 3829 Known Notation (2014牡丹江H称号)
主题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=5383 Known Notation Time Limit: 2 S ...
- ZOJ 3822 ( 2014牡丹江区域赛D题) (概率dp)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5376 题意:每天往n*m的棋盘上放一颗棋子,求多少天能将棋盘的每行每列都至少有 ...
- zoj 3822 Domination(2014牡丹江区域赛D称号)
Domination Time Limit: 8 Seconds Memory Limit: 131072 KB Special Judge Edward is the headm ...
- zoj 3822 Domination(2014牡丹江区域赛D题) (概率dp)
3799567 2014-10-14 10:13:59 Acce ...
- zoj 3829 Known Notation(2014在牡丹江区域赛k称号)
Known Notation Time Limit: 2 Seconds Memory Limit: 131072 KB Do you know reverse Polish notatio ...
- ACM学习历程—ZOJ 3868 GCD Expectation(莫比乌斯 || 容斥原理)
Description Edward has a set of n integers {a1, a2,...,an}. He randomly picks a nonempty subset {x1, ...
- ACM学习历程—ZOJ 3777 Problem Arrangement(递推 && 状压)
Description The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem sett ...
- ACM学习历程——HDU5017 Ellipsoid(模拟退火)(2014西安网赛K题)
---恢复内容开始--- Description Given a 3-dimension ellipsoid(椭球面) your task is to find the minimal distanc ...
随机推荐
- vs重复编译
VS用了这么久都没有这样的问题,昨天突然发现在自己电脑时间不对了,就调了下,以后这问题都来了.每次运行项目都要重新编译下,不管改不改底层代码.这让我很痛苦,浪费大量时间,找了好久才得到答案: .时间问 ...
- A/B测试与灰度发布
1.A/B测试与灰度发布的理论 产品是多维度的,设计体验.交互体验.系统质量.运营支持等等, 测试的目的是为了系统最终的交付,一套各方面都足够好的系统,而不是文档上定义的系统,系统是需要不断进化的. ...
- oracle img 导入dmp文件
1.新建表空间 因为我们导出的数据表的表空间不一定是USERS, 假如说是:FQDB 新建表空间SQL语句 create tablespace FQDB datafile 'c:\FQDB.dbf' ...
- 九度OJ 1352:和为S的两个数字 (查找)
时间限制:2 秒 内存限制:32 兆 特殊判题:否 提交:3160 解决:833 题目描述: 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,输 ...
- 我的Android进阶之旅------>Android视频录制小例子
============================首先看看官网上关于视频捕捉的介绍================================ Capturing videos Video ...
- setTimeout解决循环值的几种方法
for(var i=0;i<5;i++){ setTimeout(function(){ console.log(`错误 ${i}`); },0) } for(var i=0;i<5;i+ ...
- ICCV 2015 B-CNN细粒度分类
哈哈,好久没写博客了....最近懒癌发作~~主要是因为心情不太好啊,做什么事情都不太顺心,不过已经过去啦.最近一直忙着公司的项目,想用这个网络,就给大家带来了的这篇文章.可能比较老,来自ICCV 20 ...
- wap网站即手机端网页SEO优化注意事项及方法
定位和页面设计: 无论是PC端还是移动端,网站 都要考虑清楚消费群体的定位问题.虽然智能手机用户数量非常普及,但是要明白中国的大部分手机用户使用的还是2G网络,一直高 喊的3G.4G手机用户只有大约1 ...
- eclipse 修改 JDK中的src.zip的路径
http://blog.sina.com.cn/s/blog_54a1bca7010112fb.html http://www.douban.com/note/211369821/ 1.点 “wind ...
- overflow-y:auto 回到顶部
overflow-y 内容溢出元素框时发生的事情. overflow-y:auto 内容溢出元素框时自动出现滚动条,滑动滚动条显示溢出的内容. 滚动条回到顶部 var conta ...