Known Notation


Time Limit: 2 Seconds      Memory Limit: 131072 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

先算最少加入的数, 再交换。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 1111;
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
char s[N];
scanf("%s", s);
int totNum = 0, totChar = 0;
int len = (int) strlen(s);
int flag = 0;
for (int i = 0; i < len && !flag; ++i)
if (s[i] == '*') flag = 1;
if (!flag)
{
cout << 0 << endl;
continue;
}
int ans = 0;
for (int i = 0; i < len; ++i)
if (s[i] == '*') totChar++;
else totNum++;
if (totChar - totNum + 1 > 0) ans = totChar - totNum + 1;
int a[N * 5];
int curLen = len + ans;
memset(a, 0, sizeof(a));
if (ans > 0)
{
for (int i = ans + 1; i <= curLen; ++i)
if (s[i - 1 - ans] == '*') a[i] = 1;
}
else
{
for (int i = 1; i <= curLen; ++i)
if (s[i - 1] == '*') a[i] = 1;
}
if (!a[curLen])
{
int l = 1;
while (l <= curLen)
{
if (a[l]) break;
else l++;
}
swap(a[l], a[curLen]);
++ans;
}
int r = curLen, pos = 1, tot = 0;
while (pos <= curLen)
{
if (!a[pos])
{
tot++;
pos++;
}
else
{
if (tot < 2)
{
int p = -1;
for (int i = r; i >= 1 && p != -1; --i)
if (!a[i]) p = i;
swap(a[p], a[pos]);
ans++;
pos++;
tot++;
r = p - 1;
}
else
{
tot--;
pos++;
}
}
}
printf("%d\n", ans);
}
return 0;
}


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

zoj 3829 Known Notation(2014在牡丹江区域赛k称号)的更多相关文章

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

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

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

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

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

  4. zoj 3829 Known Notation

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

  5. 贪心+模拟 ZOJ 3829 Known Notation

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

  6. ZOJ 3827 Information Entropy(数学题 牡丹江现场赛)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=5381 Information Theory is one of t ...

  7. 2014年亚洲区域赛北京赛区现场赛A,D,H,I,K题解(hdu5112,5115,5119,5220,5122)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 下午在HDU上打了一下今年北京区域赛的重现,过了5题,看来单挑只能拿拿铜牌,呜呜. ...

  8. hdu5080:几何+polya计数(鞍山区域赛K题)

    /* 鞍山区域赛的K题..当时比赛都没来得及看(反正看了也不会) 学了polya定理之后就赶紧跑来补这个题.. 由于几何比较烂写了又丑又长的代码,还debug了很久.. 比较感动的是竟然1Y了.. * ...

  9. ZOJ 3827 Information Entropy (2014牡丹江区域赛)

    题目链接:ZOJ 3827 Information Entropy 依据题目的公式算吧,那个极限是0 AC代码: #include <stdio.h> #include <strin ...

随机推荐

  1. DeviceIoControl 直接从磁盘扇区读文件

    好久没写博客了,近期看了下DeviceIoControl  关于磁盘的应用,特记一文,以备久后查阅. 首先介绍下,文件在磁盘的存储结构(详细能够到网上查询NTFS文件系统相关的教程后者数据恢复方面教程 ...

  2. android实现应用程序仅仅有在第一次启动时显示引导界面

    概述 SharedPreferences的使用很easy,可以轻松的存放数据和读取数据.SharedPreferences仅仅能保存简单类型的数据,比如,String.int等.通常会将复杂类型的数据 ...

  3. MVC @Html控件

    传统的Html元素不能和服务端数据进行绑定 HtmlHelper类提供了一系列的方法来生成Html元素 并可以实现与数据绑定在一起 然后生成Html Html.BeginForm(actionName ...

  4. Zookeeper分享

    Zookeeper: 是一个分布式的,为分布式应用提供数据一致性服务的程序. Zookeeper是怎么来的? 分布式系统:是一个硬件或软件组件分布在不同的网络计算机上,彼此之间仅仅通过消息传递进行通信 ...

  5. JAVA学习JSTL与EL

    一.基础 1.EL(Expression Language):为了使jsp写起来更加简单,提供了在Jsp中简化表达式的方法 2.JSTL:(JSP Standard Tag Library)jstl标 ...

  6. 得知Android小遴选程序第七头(他们定义对话框、Gallery、ImageSwitcher)

    效果如下面的:            一共一个activity和两个xml. ******当我们须要使用的组件不在setContentView()设置的布局文件里,那我们就须要使用inflate()方 ...

  7. 自定义错误页面mvc用法

    原谅我这个新手,对大神们来说这么简单的问题,竟折腾了我一个上午,仅此文章做个记录,供以后备用. 自定义错误页面(custom error pages)在asp.net webform里的配置请看htt ...

  8. struts开发步骤

    说来惭愧.这是一个简单的struts折腾了很长一段时间,几乎相同的时间量就花了三天时间来解决.下面的步骤总结一下我开发:(我使用的是MyEclipse); 1.新建一个Exercise3的web Pr ...

  9. redis源代码解读之内存管理————zmalloc文件

    本文章主要记录本人在看redis源代码的一些理解和想法.由于功力有限,肯定会出现故障,所以.希望高手给出指正. 第一篇就是内存相关的介绍.由于我喜欢先看一些组件的东西,再看总体的流程. 先上一下代码吧 ...

  10. springMVC项目异步处理请求的错误Async support must be enabled on a servlet and for all filters involved in async

    从github上down下来一个项目,springMVC-chat.作者全是用的注解,也就是零配置.这可苦了我,经过千辛万苦,终于集成到如今的项目中有一点样子了,结果报出来以下的错误.红色部分.解决方 ...