ZOJ3829——Known Notation(贪心,模拟)

本文介绍了一种算法,用于检查给定字符串是否能表示有效的逆波兰表达式(Reverse Polish Notation, RPN)。若字符串无效,则计算使其变为有效所需的最小操作数。文中详细解释了RPN的概念,并给出了解决方案的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

Author: CHEN, Cong


首先有这样几个性质。

1.m个 * 至少需要 m+1 个数字,如果 数字数量n<m+1 那么至少要添加 m+1-n 个 数字。

2.一个序列的任意前缀都满足条件n>=m+1,则该序列合法。

3.连续的n个数字后接m个* (n>=m+1), 一定合法。

所以可以依据贪心的思想,数字尽量往前放,*尽量往后放一定是正确的。

首先可以确定最少添加的数字数量,并且把数字放到最前面。

然后扫描一遍序列,依据性质2,将不合法的*与结尾的数字交换,交换的代价一定是小于添加元素的。


#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
#define INF 0x7fffffff
using namespace std;

typedef long long LL;
const int N = 1e5 + 10;

int main()
{
    int T;
    char s[2005];
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",&s);
        int len=strlen(s);
        int op=0,num=0;
        for(int i=0; i<len; i++)
        {
            if(s[i]=='*') op++;
            else num++;
        }

        int k=max(0, op+1-num);
        int ans=k;
        
        int cnt_op=0, cnt_num=k;
        int last=len-1;
        while(last && s[last]=='*') last--;
        
        for(int i=0; i<len; i++)
        {
            if(s[i]=='*') cnt_op++;
            else cnt_num++;

            if(cnt_op+1>cnt_num)
            {
                swap(s[i], s[last]);
                while(last && s[last]=='*') last--;
                cnt_op--;
                cnt_num++;
                ans++;
            }
        }

        printf("%d\n",ans);
    }

    return 0;
}



评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值