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. It is also known as postfix notation since every operator in a 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'll offer some examples here. For instance, to add 3 and 4, one would write "3 4 +" rather than "3 + 4". If There is multiple operations, the operator is given immediately after its second operand. The arithmetic expression written "3-4 + 5" in conventional notation would being written "3 4-5 +" in Rpn:4 is first sub Tracted from 3, and then 5 added to it. Another infix expression "5 + ((1 + 2) x4)-3" can be written "the" in RPN: "5 1 2 + 4x+ 3-". An advantage of RPN was that it obviates the need for parentheses that was required by infix.
In this problem, we'll use the asterisk ' * ' as the only operator and digits from ' 1 ' to ' 9 ' (without "0") as components of operands.
You is given an expression in reverse Polish notation. Unfortunately, all space characters is missing. That means the expression is concatenated into several long numeric sequence which is separated by asterisks. Cannot distinguish the numbers from the given string.
You task was to check whether the given string can represent a valid RPN expression. If the given string cannot represent any valid RPN, please find the minimal number of operations to make it valid. There is 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 of the characters in the string. For example, if your swap the last of the 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 I S "1 2 * 34 *".
Input
There is multiple test cases. The first line of input contains an integer indicating the number of the T test cases. For each test case:
There is a non-empty string consists of asterisks and non-zero digits. The length of the string would not be 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
31*111*234***
Sample Output
102
Test instructions
Give a string of not more than 1000 length, give two kinds of operation.
Action 1: Insert any character at any location
Action 2: Swap position of any two characters in a string
Q: How to convert it to a suffix expression with minimal action
Problem Solving Ideas:
The first thing to understand is that in the string, the number on the left is more, as long as there is an operator on the right, then it is a suffix,
Instead, when the number of numeric characters in a string is greater than the number of operands, the swap operation is always more cost effective than the insert operation.
Therefore, we only need to compare the size of the two strings at the beginning, if the number of characters can not be greater than the number of characters, then the difference to the leftmost, and then the remaining string to exchange operations.
Time complexity 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-8const int inf = (1<<30)-10;us ing namespace Std;const int maxx = + 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;}
If there is a bug, please be sure to point out, thank you ~
Contact information: [Email protected]
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
2014 ACM Mudanjiang Division field Game K (ZOJ 3829)