zoj 3829 Known Notation

题目链接

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

题意:逆波兰表达式:1 1 * 表示1*1。现在给一个没有空格的逆波兰表达式,空格可以任意加(不算在操作里面),问把该波兰表达式变成合法最小需要操作多少步?

操作有两种:1,加一个数字或符号到表达式的任意位置。

      2,交换一个数字和一个符号的位置。

题解:想法题。由于空格可以任意加,所以第一个*最少需要两个数字,后面的*最少需要一个数字。那么最少需要的数字的个数就是*的个数加1。如何判断一个表达式的合法性?对于一个*来说,如果它前面的*都满足并且剩下的数字也能让它满足,那么这个*就能满足,如果表达式中所有的*都是满足的,那么表达式就是合法的。对于最后一位为数字的情况,我们只需要加一个*就行了。

再来看操作。在数字够的情况下,交换操作一定优于添加操作。假设当前*前面的数字不能让它满足,我们可以在最前面添加数字,也可以把*和后面的数字交换。我们把*和最后面的数字交换的话,首先这个*一定能满足,因为总数字个数是够的,然后我们交换了一个数字到前面,让后面的*更有可能满足。而添加数字只能让当前的*满足,不会让后面的*更优。所以交换操作一定优于添加操作,前提是数字够。

做法就是,先判断数字够不够,如果不够的话在前面添加数字。然后从前往后判断每个*的情况,如果当前*不能满足,就把这个*和最后一个数字交换。如果最后一位还是数字,那么我们就添加一个*即可。当然还要特判全是数字的情况,这种情况是合法的。

总结一下:这题关键是要想到,第一个*需要两个数字,后面的*只需要一个数字,而数字只要在*前面就行了。后面只要贪心就行。

代码如下:

#include<stdio.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<iostream>
#include<string.h>
#include<string>
#include<math.h>
#include<stdlib.h>
#define inff 0x3fffffff
#define eps 1e-8
#define nn 210000
#define mod 1000000007
typedef long long LL;
const LL inf64=LL(inff)*inff;
using namespace std;
string s;
int main()
{
    int t,i,ls,j;
    scanf("%d",&t);
    while(t--)
    {
        cin>>s;
        ls=s.size();
        int ix=0,fc=0;
        for(i=0;i<ls;i++)
        {
            if(s[i]=='*')
            {
                fc++;
            }
            else
                ix++;
        }
        if(fc==0)
        {
            puts("0");
            continue;
        }
        int ans=0;
        if(fc+1>ix)
        {
            ans+=fc+1-ix;
            ix=fc+1-ix;
        }
        else
            ix=0;
        fc=0;
        j=ls-1;
        for(i=0;i<ls;i++)
        {
            if(s[i]=='*')
            {
                fc++;
                if(fc+1>ix)
                {
                    for(;j>i;j--)
                    {
                        if(s[j]!='*')
                        {
                            s[j]='*';
                            ix++;
                            fc--;
                            ans++;
                            break;
                        }
                    }
                }
            }
            else
                ix++;
        }
        if(s[ls-1]!='*')
            ans++;
        printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值