您的位置:首页 > 其它

2014 ACM-ICPC牡丹江现场赛K题题解

2014-12-16 16:01 309 查看
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:

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

传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5383

题目大意:给你一个T(T组数据),每组数据只有一个字符串S(只有数字1~9和*),有三种操作:1:向字符串中任何位置插入一个数字;2:向字符串中任何位置插入一个*;3:将字符串中任意两个位置的字符进行交换。要求最后操作成一个正确的后缀表达式,输出最小的操作数。
题目分析:首先从总体来分析,要使串满足条件,至少的要求是数字>运算符。所以先将少了的数字补充到串的前面(显然这是最优且无后效性的)。然后我们从串的左边向右边扫过去,当是数字的时候我们就直接记录下来(记录前面还剩多少数字,便于与后面的运算符匹配);如果是运算符的话就得分类讨论了:如果数字<2(不够运算),这时我们应该和后面的数字交换呢,还是向前面添加数字好呢?显然向后面交换(和最后一个数字交换)是不会有什么后效性的,如果先前添加数字的话就会导致很多麻烦(例如:1*11,就要2步,不满足最优),所以这种情况用交换更优。当数字>1(能进行运算)则直接运算,前面记录的数字减1(先用最少的数字把运算符抵消掉),同时判断一下当前是不是已经达到了串的最尾端(如果是的话,可以直接将前面存下来的数字直接运算成一个数字),一直这样处理完字符串即可。
PS:如果最后只剩下数字,这时候会碰到这种情况:前面进行过运算(运算过的部分是不能再和其他数字结合在一起组成一个数的,还需要一个运算符才是合理的后缀表达式)和前面没进行过运算(这时候只要一个数字,不需要操作),所以要标记处理一下这个!


#include<cmath>
#include<string>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int INF=(1<<31)-10;
const int MOD=(1e9)+7;
char s[1100];
void swap(int i,int j)
{
char h;
h=s[i];s[i]=s[j];s[j]=h;
}
int main()
{
int i,j,t,bo,bb,len,num,mul,ans;
scanf("%d", &t);
getchar();
while (t--) {
gets(s);
len=strlen(s);
bo=0;bb=0;
num=0;mul=0;ans=0;
for (i=0;i<len;i++)
if (s[i]=='*') mul++;
else num++;
if (num<=mul) {
ans+=mul-num+1;num=mul-num+1;
} else num=0;
j=len-1;
for (i=0;i<len;i++)
if (s[i]!='*') num++;
else {
if (num<2) { ///数字小于2时,去后面换数字或者后面只剩运算符了直接退出
while (j>=0&&s[j]=='*') j--;
if (j>i) {
swap(i,j);ans++;num++;
} else {
if (len-i>=num) ans=ans+(len-i-num+1);
bo=1;
break;
}
if (bo) break;
} else {
bb=1;///前面计算过一次!
if (i==len-1) num=1;
else num--;
}
}
if (num>1&&bb) ans++;
printf("%d\n", ans);
}
return 0;
}


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: