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

题目链接:Known Notation

解题思路:贪心。假设num < star 时,则必须在前面补充  star - num + 1  个数字,由于star个星星,须要star+1个数字,才符合要求。接下来,尽量把数字放到前面,把星星放到后面,两个数字能够消掉一个星星,由于这时候 a*b 相当于一个数字了。假设前面的数字不够用,就用前面的星星和后面的数字交换,由于交换比插入的结果要好。不断贪心下去,就可以。

AC代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define INF 0x7fffffff int main()
{
#ifdef sxk
freopen("in.txt","r",stdin);
#endif
int n;
string s;
scanf("%d",&n);
while(n--)
{
int num = 0, star = 0;
cin>>s;
int len = s.size();
for(int i=0; i<len; i++){
if(s[i] == '*') star ++;
else num ++;
} int left_num = 0, ans = 0;
if(num <= star){
left_num += star - num + 1;
ans += left_num;
} for(int i=0, p = len-1; i<len; i++){
while(i < p && s[p] == '*') p --;
if(s[i] == '*'){
left_num --;
if(left_num < 1){
swap(s[i], s[p]); //前面的数字不够,用前面的星星和后面的数字交换
ans ++;
p --;
left_num += 2;
}
}
else left_num ++;
}
cout<<ans<<endl;
}
return 0;
}

2014ACM/ICPC亚洲区域赛牡丹江站现场赛-K ( ZOJ 3829 ) Known Notation的更多相关文章

  1. 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-A ( ZOJ 3819 ) Average Score

    Average Score Time Limit: 2 Seconds      Memory Limit: 65536 KB Bob is a freshman in Marjar Universi ...

  2. 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-I ( ZOJ 3827 ) Information Entropy

    Information Entropy Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Information ...

  3. 2014ACM/ICPC亚洲区域赛牡丹江站汇总

    球队内线我也总水平,这所学校得到了前所未有的8地方,因为只有两个少年队.因此,我们13并且可以被分配到的地方,因为13和非常大的数目.据领队谁oj在之上a谁去让更多的冠军.我和tyh,sxk,doub ...

  4. 2014ACM/ICPC亚洲区域赛牡丹江现场赛总结

    不知道怎样说起-- 感觉还没那个比赛的感觉呢?如今就结束了. 9号.10号的时候学校还评比国奖.励志奖啥的,由于要来比赛,所以那些事情队友的国奖不能答辩.自己的励志奖班里乱搞要投票,自己又不在,真是无 ...

  5. 2015 ACM / ICPC 亚洲区域赛总结(长春站&北京站)

    队名:Unlimited Code Works(无尽编码)  队员:Wu.Wang.Zhou 先说一下队伍:Wu是大三学长:Wang高中noip省一:我最渣,去年来大学开始学的a+b,参加今年区域赛之 ...

  6. 2013ACM/ICPC亚洲区南京站现场赛---Poor Warehouse Keeper(贪心)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4803 Problem Description Jenny is a warehouse keeper. ...

  7. 2016 ACM/ICPC亚洲区青岛站现场赛(部分题解)

    摘要 本文主要列举并求解了2016 ACM/ICPC亚洲区青岛站现场赛的部分真题,着重介绍了各个题目的解题思路,结合详细的AC代码,意在熟悉青岛赛区的出题策略,以备战2018青岛站现场赛. HDU 5 ...

  8. 2016ACM/ICPC亚洲区大连站现场赛题解报告(转)

    http://blog.csdn.net/queuelovestack/article/details/53055418 下午重现了一下大连赛区的比赛,感觉有点神奇,重现时居然改了现场赛的数据范围,原 ...

  9. Building Fire Stations 39届亚洲赛牡丹江站B题

    题意:      给你一棵树,让你再里面选取两个点作为**点,然后所有点的权值是到这两个点中最近的那个的距离,最后问距离中最长的最短是多少,输出距离还有那两个点(spj特判). 思路:      现场 ...

随机推荐

  1. js实现表格的选中一行-------Day58

    最開始想很多其它的用js来动态操作表格,是由于在应用了easyUI之后,发现直接写一个<table id="tt"></table>,这就够了,界面里面就剩 ...

  2. QT---系统托盘图标不显示原因

    很久没用QT写UI相关的东西了,有些东西都忘记了,今天竟然忘记了系统托盘图标是怎么显示的了.下面说下解决方法 1.现象, 设置了QSystemTrayIcon的Icon图标,但就是不显示自己设置的图片 ...

  3. <转载>使用css让大图片不超过网页宽度

    让大图片不超过网页宽度,让图片不撑破通过CSS样式设置的DIV宽度! 接下来,我们来介绍下网站在开发DIV+CSS的时候会遇到一个问题,在发布一个大图片的时候因为图片过宽会撑破自己设置的div宽度的问 ...

  4. zabbix 监控jmx 需要--enable-java

    安装Javagateway如果原来已经安装zabbix,只需要再添加以下zabbix-java # tar zxvf zabbix-2.2.0.tar.gz # cd zabbix-2.2.0 # . ...

  5. shell字符串长度

    方法一 $ expr length "Find out the length of this string from Linux Bash shell." 57 方法二 str1= ...

  6. hbulider mui框架

    1.webview http://www.dcloud.io/docs/api/zh_cn/webview.shtml#plus.webview.WebviewStyle http://www.dcl ...

  7. CF437D(The Child and Zoo)最小生成树

    题目: D. The Child and Zoo time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  8. Swift - 让StoryBoard设计视图,程序运行时都使用横屏形式

    1,运行时横屏 将项目属性“General”->“DeviceOritentation”的Portrait复选框去掉 2,storyboard设计视图横屏 在storyboard中,单击中间界面 ...

  9. Bootstrap,Foundation和TypeScript

    http://www.oschina.net/question/12_128155 http://www.oschina.net/news/72330/typescript-2-0 给自己提个醒,随时 ...

  10. Qt中使用定时器(可使用QObject::timerEvent定时执行,QTimer::singleShot可只触发一次)

    在Qt中使用定时器有两种方法,一种是使用QObiect类的定时器:一种是使用QTimer类.定时器的精确性依赖于操作系统和硬件,大多数平台支持20ms的精确度 1.QObject类的定时器 QObje ...