Mess:
1. The number of numbers is more than the number of *, assuming that the numbers need to fill the numbers first
2. The optimal structure should be the numbers on the left, * all on the right
3. Sweep from left to right, encounter number +1, encounter *-1, assuming the current value <1 this * and the last digit to exchange position
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
Author:CHEN, Cong
Source:The acm-icpc Asia Mudanjiang regional Contest
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace Std;char Str[2000];int Main () {int t_t; scanf ("%d", &t_t); while (t_t--) {scanf ("%s", str); int Len=strlen (str); int c=0,start=0; BOOL Flag=false; for (int i=0;i<len;i++) {if (str[i]== ' * ') {flag=true; c--; start++; } else C + +; } int ans=0; if (c<=0) ans=start+1-(Len-start); C=ans; for (int i=0;i<len;i++) {if (str[i]== ' * ') c--; else C + +; if (c<1) {for (int j=len-1;j>i;j--) {if (str[j]!= ' * ') {swap (str[i],str[j]); Break }} ans++; c+=2; } } if (str[len-1]!= ' * ' &&flag) ans++; printf ("%d\n", ans); } return 0;}
ZOJ 3829 known Notation wtf