乱搞:

1.数字的个数要比*的个数多一个,假设数字不足须要先把数字补满

2.最优的结构应该是数字都在左边,*都在右边

3.从左往右扫一遍,遇到数字+1,遇到*-1,假设当前值<1则把这个*和最后面的一个数字交换位置

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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std; char str[2000]; int main()
{
int T_T;
scanf("%d",&T_T);
while(T_T--)
{
scanf("%s",str);
int len=strlen(str);
int c=0,start=0;
bool flag=false;
for(int i=0;i<len;i++)
{
if(str[i]=='*')
{
flag=true;
c--;
start++;
}
else c++;
}
int ans=0;
if(c<=0) ans=start+1-(len-start);
c=ans;
for(int i=0;i<len;i++)
{
if(str[i]=='*') c--;
else c++;
if(c<1)
{
for(int j=len-1;j>i;j--)
{
if(str[j]!='*')
{
swap(str[i],str[j]);
break;
}
}
ans++;
c+=2;
}
}
if(str[len-1]!='*'&&flag) ans++;
printf("%d\n",ans);
}
return 0;
}

ZOJ 3829 Known Notation 乱搞的更多相关文章

  1. zoj 3829 Known Notation

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

  2. 贪心+模拟 ZOJ 3829 Known Notation

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

  3. ZOJ 3829 Known Notation 贪心

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

  4. ZOJ 3829 Known Notation (2014牡丹江H称号)

    主题链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=5383 Known Notation Time Limit: 2 S ...

  5. 【贪心+一点小思路】Zoj - 3829 Known Notation

    借用别人一句话,还以为是个高贵的dp... ... 一打眼一看是波兰式的题,有点懵还以为要用后缀表达式或者dp以下什么什么的,比赛后半阶段才开始仔细研究这题发现贪心就能搞,奈何读错题了!!交换的时候可 ...

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

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

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

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

  8. 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 ...

  9. ZOJ - 3829 Known Notation(模拟+贪心)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 给定一个字符串(只包含数字和星号)可以在字符串的任意位置添加一个数字 ...

随机推荐

  1. BZOJ 1875: [SDOI2009]HH去散步( dp + 矩阵快速幂 )

    把双向边拆成2条单向边, 用边来转移...然后矩阵乘法+快速幂优化 ------------------------------------------------------------------ ...

  2. Eclipse安装反编译插件JD(Java Decompiler)

    JD安装说明:*****Eclipse 插件安装*****1. 在网上搜索并下载jdeclipse_update_site.zip2. Eclipse -> Install New Softwa ...

  3. git 使用详情

    一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的区别? SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以 ...

  4. QT学习 之 三维饼图绘制

    QT里没有相应统计图形的绘制组件 只有手工自己画 效果如图 void aaq::paintEvent( QPaintEvent* ev ) { QPainter painter(this); // 去 ...

  5. ThinkPHP 3.1.2 模板中的基本语法<2>

    本节课大纲: 一.导入CSS和JS文件 1.css link js scr <link rel='stylesheet' type='text/css' href='__PUBLIC__/Css ...

  6. gcc/g++ 如何支持c11 / c++11标准编译

    如果用命令 g++ -g -Wall main.cpp  编译以下代码 : /* file : main.cpp */ #include <stdio.h> int main() { in ...

  7. linux 命令之sar——监视系统状态

    摘要:在进行系统或者内核测试的时候,我们经常需要观察cpu利用率,缓冲区使用情况,文件读写情况等等.在linux系统下,我们可以用sar命令来达到这个要求. sar 命令行的常用格式: sar [op ...

  8. activity变成Dialog的步骤

    1.在布局文件上最外层最好使用RelativeLayout来布局,如果使用LinearLayout来布局的话,显示对话框的话,感觉会有点问题: 要在预览中看到框框,并且是match_parent的,而 ...

  9. ios中利用NSDateComponents、NSDate、NSCalendar判断当前时间是否在一天的某个时间段内。

    应用中设置一般会存在这样的设置,如夜间勿扰模式,从8:00-23:00,此时如何判断当前时间是否在该时间段内.难点主要在于如何用NSDate生成一个8:00的时间和23:00的时间,然后用当前的时间跟 ...

  10. 在Mac上配置/使用Github

    文/天才晓波(简书作者)原文链接:http://www.jianshu.com/p/20eee155bbee著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 先简单介绍一下Git和Git ...