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

看来只要认真思考,这些较为简单的题都是可以A的。

想了一个上午,一开始没把逆波兰表达式的可能性想清楚,导致算法错误,后来想到只要前面数字的个数大于*的个数,都是成立的,而且交换要比增加效率高。只有当数字个数小于*个数的时候才会增加数字,而且增加在最前面,这样是最优方案。之后只要发现到某一位*的个数大于等于数字的个数了,就把*和最后一个数字交换就可以了。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define M(a,b) memset(a,b,sizeof(a))
typedef long long LL;
using namespace std; char num[]; int main()
{
int t;
int ans = ;
scanf("%d",&t);
while(t--)
{
scanf("%s",num);
int sl = strlen(num);
int cnt1 = ;
int cnt2 = ;
int flag = -;
int bu = ;
int seg = ;
int step = ;
int save = -;
for(int i = sl-;i>=;i--)
{
if(num[i]=='*')
save = i;
}
if(num[sl-] != '*')
{
if(save!=-)
{swap(num[sl-],num[save]);
step++;}
}
int tm1 = ;
int tm2 = ;
int ed;
for(int i = ;i<sl;i++)
{
if(num[i]=='*') tm1++;
else tm2++;
}
if(tm1>=tm2) {
step+=(tm1-tm2+);
cnt2=(tm1-tm2+);
ed = cnt2;
}
//cout<<ed<<endl;
for(int i = ;i<sl;i++)
{
if(num[i]=='*')
{
cnt1++;
if(cnt1>=cnt2)
{
int te1,te2;
for(int p = ;p<sl;p++)
if(num[p] != '*') te1 = p;
for(int q = sl-;q>=;q--)
if(num[q] == '*') te2 = q;
swap(num[te1],num[te2]);
step++;
cnt1 = ;
cnt2 = ed;
i = -;
continue;
}
}
else cnt2++;
}
printf("%d\n",step);
}
return ;
}

2014牡丹江K Known Notation的更多相关文章

  1. 2014 牡丹江区域赛 B D I

    http://acm.zju.edu.cn/onlinejudge/showContestProblems.do?contestId=358 The 2014 ACM-ICPC Asia Mudanj ...

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

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

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

  4. [2014 Regional]牡丹江 H Hierarchical Notation 做题记录

    主妇:老年人谁是炮灰牡丹江,我们的团队只是做同步大赛 他决定开爆震H什么时候,A 5min 1Y.I在该限制后,纠结了很久30min+ 1Y,神继续承担各种位置卡D在,hpp见B我认为这是非常熟悉的研 ...

  5. 2014牡丹江——Hierarchical Notation

    problemId=5380" style="background-color:rgb(51,255,51)">题目链接 字符串模拟 const int MAXN ...

  6. 2014牡丹江——Known Notation

    题目链接 题意: 输入一个长度不超过1000的字符串,包含数字(1-9)和星号(*).字符串中的空格已经丢失,所以连起来的数字串能够看成很多分开的数.也能够看成连续的数,即能够随意加入空格. 如今有两 ...

  7. The 2014 ACM-ICPC Asia Mudanjiang Regional Contest(2014牡丹江区域赛)

    The 2014 ACM-ICPC Asia Mudanjiang Regional Contest 题目链接 没去现场.做的网络同步赛.感觉还能够,搞了6题 A:这是签到题,对于A堆除掉.假设没剩余 ...

  8. 2014牡丹江网络zoj3816Generalized Palindromic Number(dfs或者bfs)

    #include <iostream> #include <stdio.h> #include <cmath> #include <algorithm> ...

  9. 2014牡丹江网络赛ZOJPretty Poem(暴力枚举)

    /* 将给定的一个字符串分解成ABABA 或者 ABABCAB的形式! 思路:暴力枚举A, B, C串! */ 1 #include<iostream> #include<cstri ...

随机推荐

  1. nagios note

    server: nagios nagios_plugin nrpe ip:192.168.1.2 client nagios_plugin xinetd nrpe ip:192.168.1.3 一篇介 ...

  2. iOS 采用个推时,未收到推送消息,测试DeviceToken无效

    一般在调试时我们使用任何boundleID,即为*的profile文件 但在使用推送后进行调试,除了创建和上传APN证书,还需要重新生成 (1)特定程序的BoundleID (2)包含Push Not ...

  3. CentOS下X Window与命令行界面的切换

    [Ctrl] + [Alt] + F1~F6:文字界面登陆tt1~tty6 [Ctrl] + [Alt] + F7:图形界面桌面 从文字界面启动图形界面的命令:startx

  4. 一个WPF控件 诡异的MouseEvent 。

    背景: private System.Windows.Controls.Border _borderTouch; private bool _mouseDown = false;  private S ...

  5. PHP常用函数备用

    刚学习php的时候,我也为记忆php函数苦恼不已.认为干嘛记忆这么枯燥无味的东西呢?用的时候查一下手册不就行了吗?但是当时因为身在辅导机构,还是记忆了一大堆自己并不感兴趣的函数. 由此就想起来,小的时 ...

  6. HTML5学习总结-11 IOS 控件WebView显示网页

    一 加载外部网页 1.使用UIWebView加载网页 运行XCode  新建一个Single View Application . 2 添加安全消息 添加以下消息到项目的  Info.plist &l ...

  7. zabbix监控系列(4)之zabbix报警邮件无法发送

    情况介绍 首先确保邮箱规则没有把报警邮件作为垃圾邮件拉黑了. 服务器断电重启后,发现zabbix报警邮件无法发送,断电之前是好好的,但是重启后不行了,于是查看maillog日志,发现这个错误: Hos ...

  8. 机器学习笔记--KNN算法1

    前言 Hello ,everyone. 我是小花.大四毕业,留在学校有点事情,就在这里和大家吹吹我们的狐朋狗友算法---KNN算法,为什么叫狐朋狗友算法呢,在这里我先卖个关子,且听我慢慢道来. 一 K ...

  9. chown -R命令的使用

    chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者用户ID:组可以是组名或者组ID:文件是以空格分开的要改变权限的文件列表,支持通配符.系统管理员经常使用chown命令,在将文件拷贝 ...

  10. java编程思想-java集合总结-基本概念

    1.java 容器类类库的用途是"保存对象",并将其划分为两个不同的概念: 1)Collection.一个独立元素的序列,这些元素都服从一条或多条规则.List 必须按照插入的顺序 ...