主题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?

problemId=5383

Known Notation


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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

3
1*1
11*234**
*

Sample Output

1
0
2

Author: CHEN, Cong

Source: The 2014 ACM-ICPC Asia Mudanjiang Regional Contest

problemId=5383" style="color:blue; text-decoration:none">Submit    

problemId=5383" style="color:blue; text-decoration:none">Status

近期忙着各种事情。好久没写博客了。。。

这道题是牡丹H题,当时一直纠结一道概率DP。

。。这题并没有深入的想,到比赛后才搞出来。

。。

首先我们能够先对字符串进行补全,由于数字数目必须是’*‘数目+1。之后呢,我们发现对于‘*’,我们

事实上仅仅要把它尽可能的丢到后面去就完了。。

然后从头到尾扫一遍。

只是有些特殊情况记得要小心。。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<vector>
#include<bitset>
#include<cstdlib>
#define CLR(A) memset(A,0,sizeof(A))
using namespace std;
int main(){
int T;
while(~scanf("%d",&T)){
while(T--){
string str;
cin>>str;
int cnum=0,cop=0,ans=0;
for(int i=0;str[i];i++){
if(str[i]>='0'&&str[i]<='9') cnum++;
else cop++;
}
if(cnum<cop+1) {
ans+=(cop+1-cnum);
cnum=cop+1-cnum;
}
else cnum=0; if(cop==0){
cout<<"0"<<endl;
continue;
}
int len=str.size();
int last=len-1;
bool flag=0;
while(last>=0&&!(str[last]>='0'&&str[last]<='9')) last--;
for(int i=0;str[i];i++){
if(i>=last) break;
if(str[i]>='0'&&str[i]<='9'){ cnum++;}
else{
if(cnum<2){
ans++;
cnum++;
last--;
flag=1;
while(last>=0&&!(str[last]>='0'&&str[last]<='9')) last--;
}
else cnum--;
}
}
if(flag==0&&str[len-1]!='*') ans++;
cout<<ans<<endl;
}
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

ZOJ 3829 Known Notation (2014牡丹江H称号)的更多相关文章

  1. zoj 3829 Known Notation(2014在牡丹江区域赛k称号)

    Known Notation Time Limit: 2 Seconds      Memory Limit: 131072 KB Do you know reverse Polish notatio ...

  2. zoj 3829 Known Notation

    作者:jostree 转载请说明出处 http://www.cnblogs.com/jostree/p/4020792.html 题目链接: zoj 3829 Known Notation 使用贪心+ ...

  3. 贪心+模拟 ZOJ 3829 Known Notation

    题目传送门 /* 题意:一串字符串,问要最少操作数使得成为合法的后缀表达式 贪心+模拟:数字个数 >= *个数+1 所以若数字少了先补上在前面,然后把不合法的*和最后的数字交换,记录次数 岛娘的 ...

  4. ACM学习历程——ZOJ 3829 Known Notation (2014牡丹江区域赛K题)(策略,栈)

    Description Do you know reverse Polish notation (RPN)? It is a known notation in the area of mathema ...

  5. ZOJ 3829 Known Notation(字符串处理 数学 牡丹江现场赛)

    题目链接:problemId=5383">http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5383 Do you ...

  6. zoj 3822 Domination(2014牡丹江区域赛D称号)

    Domination Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge Edward is the headm ...

  7. ZOJ 3829 Known Notation 贪心

    Known Notation Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/showPro ...

  8. ZOJ 3829 Known Notation 乱搞

    乱搞: 1.数字的个数要比*的个数多一个,假设数字不足须要先把数字补满 2.最优的结构应该是数字都在左边,*都在右边 3.从左往右扫一遍,遇到数字+1,遇到*-1,假设当前值<1则把这个*和最后 ...

  9. ZOJ 3829 Known Notation 贪心 难度:0

    Known Notation Time Limit: 2 Seconds      Memory Limit: 65536 KB Do you know reverse Polish notation ...

随机推荐

  1. JLINK烧写BIN文件到nand、norflash、SDRAM

    1. 简要说明 JLink的调试功能.烧写Flash的功能都很强大,但是对于S3C2410.S3C2440的Flash操作有些麻烦:烧写Nor Flash时需要设置SDRAM,否则速率很慢:烧写Nan ...

  2. QtQml 应用程序的性能考虑与建议(来自小V的翻译)

    QtQml 应用程序的性能考虑与建议 原文:csdn aidear_evo QtQml应用程序的性能考虑与建议 本文翻译自Qt官网文档:http://doc.qt.io/qt-5/qtquick-pe ...

  3. 取PE文件的引入表和导出表

    直接上代码(这里列出C++和Delphi的代码),Delphi代码中包含导入及导出文件和函数列表,PE结构可参阅资料,很多很详细,需要注意的是,本例中是映射到内存,不是通过PE装载器装入的,所以对于节 ...

  4. ORACLE RAC中一个实例不能随crs自动启动的解决

    现象:在两个节点上做CRS的重启,这个实例都不能随CRS的启动而启动.CRS启动后做crs_start -all可以把没启动的资源起来,而且无报错. 分析:去crsd.log中找原因,发现CRS根本就 ...

  5. 获取枚举Name,Value,Description两种方法

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...

  6. Visual Studio使用技巧

    编程部分: 1.TODO:书签 打开之后返回上次工作的位置.让我们不再用脑子去记忆,去回顾刚刚工作到的部分. 操作非常easy.例如以下代码所看到的: public DataTable SelectB ...

  7. 得到文件的MD5值

    /// <summary> /// 得到文件的MD5值 /// </summary> /// <param name="Path">文件路径&l ...

  8. 图片的像素和Android的dp值之间的关系。

    这是一个困扰我很就得问题.今天在我的反复摸索下,总结出了一些个规律. 以下测试以魅族mx5为例. 手机参数:5.5英寸:高:1920:宽1080. /** * 获得屏幕的宽度 * * @param c ...

  9. 在EL表达式或者Struts标签库中格式化日期对象,即将Date转换为yyyy-MM-dd格式

    一.EL表达式 首先,在jsp页面引入<fmt> tags,<%@ taglib prefix="fmt" uri="http://java.sun.c ...

  10. iOS状态栏字体设置为白色

    info.plist 添加字段: view controller -base status bar appearence 设为NO [[UIApplication sharedApplication] ...