2014年ACM牡丹江赛区现场赛K题(ZOJ 3829)

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

题意:

给一个长度不超过1000的字符串,给出两种操作。

操作1:在任意位置插入任意字符

操作2:交换字符串中的任意两个字符的位置

问:如何用最少的操作将其转换成后缀表达式的形式

解题思路:

首先要明白,字符串中,左边的数字再多,只要右边有一个操作符,那么便算是一个后缀式,

而:在字符串中数字字符个数 大于 操作字符个数时,交换操作总是比插入操作更加划算。

因此,我们只需要在一开始时比较一下两种字符串的大小,如果数字字符数量不能大于操作字符数量,那么把差值补齐在最左边,然后再剩下的字符串中进行交换操作即可。

时间复杂度O(n)

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <sstream>
#define PI acos(-1.0)
#define eps 1e-8
const int  inf =  (1<<30) - 10;
using namespace std;

const int maxx = 1000 + 10;
int T;
int ans;
char str[maxx];

int main() {
    //freopen("input.txt","r",stdin);
    cin>>T;
    while(T--) {
        scanf("%s",str);
        int t1 = 0;
        int t2 = 0;
        int p = strlen(str);
        for(int i = 0; i < p; ++i) {
            if(str[i] == '*') {
                t1++;
            } else {
                t2++;
            }
        }
        ans = 0;
        int l = 0;
        int lnum = 0;
        int r = p-1;
        if(t2<=t1) {
            lnum = t1 - t2 + 1;
            ans += lnum;
        }
        for(; l <= r; ++l) {
            if(str[l] == '*') {
                if(lnum>=2) {
                    lnum--;
                } else {
                    ans++;
                    while(l <= r) {
                        if(str[r] != '*') {
                            swap(str[l],str[r]);
                            lnum++;
                            r--;
                            break;
                        }
                        r--;
                    }
                }
            } else {
                lnum++;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

如有BUG,请大家务必指出,不胜感激~

联系方式:[email protected]


版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-03 13:28:00

2014年ACM牡丹江赛区现场赛K题(ZOJ 3829)的相关文章

2014ACM/ICPC亚洲区域赛牡丹江站现场赛-K ( 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 expres

ZOJ3822 ACM-ICPC 2014 亚洲区域赛牡丹江赛区现场赛D题Domination 概率DP(两种解法)

题目地址:点击打开链接 这道题有两种做法,第一种是直接求期望,类似于poj 2096 区别在于这个步数有限.所以要迭代步数. #include <cstdio> #include <cstring> #include <iostream> #define maxn 55//这里刚开始写成了50+10 那么maxn*maxn就会小很多wa了一次 using namespace std; double dp[maxn][maxn][maxn*maxn]; int N,M,T

ACM-ICPC 2018 青岛赛区现场赛 K. Airdrop &amp;&amp; ZOJ 4068 (暴力)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4068 题意:吃鸡游戏简化为二维平面上有 n 个人 (xi,yi),空投的位置在 (x0,y0),每一秒所有人向靠近空投的位置走一步,四个方向有优先级先后(优先纵坐标),若已经在空投的位置则不变.往空投位置移动过程中,若两个或者更多人在同一点相遇(在空投相遇不算),则他们都死亡.给出空投的纵坐标,询问空投在所有横坐标中最少和最多存活的人数是多少. 题解:在最优情况

2013 ACM/ICPC 长沙现场赛 A题 - Alice&#39;s Print Service (ZOJ 3726)

Alice's Print Service Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice is providing print service, while the pricing doesn't seem to be reasonable, so people using her print service found some tricks to save money. For example, the price when

HDU 4119 Isabella&#39;s Message (2011年成都赛区现场赛I题)

1.题目描述:点击打开链接 2.解题思路:本题是一道模拟题,要求模拟一个解密的过程,练习这么久第一次做模拟题1Y了,内心还是很激动的~.只需要根据题意,记录* 所在的位置即可,然后每次都是先解密,后顺时针旋转90度.把每次解密的信息放到一个vector里,接下来就是连接它们,得到解密后的字符串,在map中查找这些单词是否存在即可.如果都存在,就把这条解密信息放到ans中,最后对ans排序,输出ans[0]就是答案. 3.代码: //#pragma comment(linker, "/STACK:

2014 ACM/ICPC 牡丹江赛区现场赛

最近突然感觉状态不佳,可能是天冷的缘故? 赛后来做牡丹江赛区的题目 [A]3819 Average Score -- 签到题 [B] [C] [D] [E] [F] [G] [H] [I] [J] [K]3829 Known Notation -- 贪心 + 模拟 [A]3819 Average Score -- 签到题 Average Score Time Limit: 2 Seconds                                     Memory Limit: 65

HDU 5128 The E-pang Palace(2014广州赛区现场赛B题 计算几何)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5128 解题报告:在一个平面上给出n个点的坐标,用这n个点作为矩形的四个顶点,作两个矩形,要求两个矩形不能相交,也不能有边和点相交,然后两个矩形的面积之和要最大,求最大的面积之和是多少?如果不存在输出imp 因为n <=30,所以可以先把所有的矩形枚举出来,然后再暴力判断两两矩形组合,首先要满足位置关系,然后取面积和最大就是了.要注意的地方就是要小心一个大矩形包含一个小矩形的情况,在这种情况下,是满足

HDU 5074 Hatsune Miku(2014鞍山赛区现场赛E题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5074 解题报告:给出一个长度为n的序列,例如a1,a2,a3,a4......an,然后这个序列的美丽值就是socre[a1][a2] + socre[a2][a3] + ..... socre[an-1][an],但是这个序列里面并不是所有的数都是确定的,输入包含一些大于0的数和一些-1,-1表示这个数可以任意,但是要在m的范围内,给出socre[i][j],求这个序列最大的美丽值. 一个二维dp

HDU 5122 K.Bro Sorting(2014北京区域赛现场赛K题 模拟)

这题定义了一种新的排序算法,就是把一串序列中的一个数,如果它右边的数比它小 则可以往右边移动,直到它右边的数字比它大为止. 易得,如果来模拟就是O(n^2)的效率,肯定不行 想了一想,这个问题可以被转化成 求这一串序列当中每个元素,它的右边是否存在小于它的数字,如果存在,则++ans 一开始没想到诶= = 不应该不应该 1 //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler 2 #include <std