题目链接:

problemId=5383">http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5383

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

题意:

给出两个操作:插入和随意交换字符串两个字母;

求最少的操作数使得字符串变成后缀表达式,

PS:

仅仅有缺少数字的时候才会用到插入,其它就是交换操作,把*都交换到后面。而一个*须要两个数字。

代码例如以下:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 2017;
int main()
{
int t;
char s[maxn];
scanf("%d",&t);
while(t--)
{
scanf("%s",s);
int len = strlen(s);
int cont_num = 0, cont_star = 0;
for(int i = 0; i < len; i++)
{
if(s[i] == '*')
cont_star++;
else
cont_num++;
}
if(len == cont_num)
{
printf("0\n");
continue;
}
int k = cont_star-cont_num+1;
if(k < 0)
k = 0;
int star = 0;
int num = k;
int ans = k;
int re[maxn];
for(int i = len-1, l = 0; i >= 0; i--)
{
if(s[i]!='*')
re[l++] = i;
}
int l = 0;
for(int i = 0; i < len; i++)
{
if(s[i]=='*')
star++;
else
num++;
if(star+1 > num)
{
swap(s[i],s[re[l++]]);
ans++;
star--;
num++;
}
}
if(s[len-1] != '*')
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(模拟+贪心)

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

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

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

  5. 2014ACMICPC亚洲区域赛牡丹江现场赛之旅

    下午就要坐卧铺赶回北京了.闲来无事.写个总结,给以后的自己看. 因为孔神要保研面试,所以仅仅有我们队里三个人上路. 我们是周五坐的十二点出发的卧铺,一路上不算无聊.恰巧邻床是北航的神犇.于是下午和北航 ...

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

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

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

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

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

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

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

随机推荐

  1. Winform 异步调用

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  2. BZOJ 4800 折半暴搜

    思路: 把它拆成两半  分别搜一发 两部分分别排好序 用two-pointers扫一遍 就可以了. (读入也要用long long) //By SiriusRen #include <cstdi ...

  3. Java中last_insert_id的使用

    last_insert_id的作用是:在当前表中的主键是自增时,插入一条新记录的同时使用last_insert_id可以获取当前的新记录的主键id. 下面是一个例子: import java.sql. ...

  4. linux 清空文件的几种方案

    之前要清理文件,都是简单粗暴的rm -rf log文件,最近,发现在某些环境下,是不能删除文件本省的,又必须要清理文件的内容信息,经过亲自实验,目测以下的几种方案是可行的,方案如下: 1.采用vi命令 ...

  5. C#屏蔽Alt+F4,解决Alt+F4关闭窗体时对话框NO后,线程不退出问题

    //1.将窗体的属性KeyPrieview设置为true //keypreview属性为true的时候,就可以响应键盘事件v //2.在窗体KeyDown事件中加如下代码 private void F ...

  6. Python之first script

    1 A first script 1) script1.py - imports a Python module (libraries of additional tools) to fetch th ...

  7. SQL Server存储过程作业(二)

    阶段1:练习——统计某类型客房的入住客人人数 需求说明 使用存储过程统计在指定类型的客房入住客人的总人数 提示: 存储过程的输入参数是指定的客房类型名称 USE Hotel GO --阶段1:查询入住 ...

  8. Visual Studio 推荐插件以及一些设置

    Microsoft Visual Studio 作为微软自家的 IDE 集成 NuGet 包管理器,是的引用一些不同版本组件DLL十分方便. 集成编译环境,强大的 Debug 断点.附加功能 项目模板 ...

  9. Linux下Shell脚本输出带颜色文字

    文本终端的颜色可以使用“ANSI非常规字符序列”来生成.举例: echo -e "\033[44;37;5m ME \033[0m COOL" 以上命令设置作用如下: 背景色为蓝色 ...

  10. C# Cookies设置和读取

    public ActionResult Index() { #region 写入Cookies HttpCookie cookie = new HttpCookie("CookieName& ...