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:
- 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 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.如果该字符串全部为数字,则无需操作,直接输出0。 2.连续的n个数字后面接连续的m个*,当n>m时,一定是有效的答案。从而最终的目的就是把最后的数字尽量向前移动, 把最前面非法的*尽量向后移动,因此insert操作添加数字时,只需添加在最前面即可。 3.*的个数一定要小于数字的个数,因为每个*号需要消耗掉一个数字,并且首个*消耗掉2个数字。因此如果发现数字的个数比*的个数少k个, 那么需要直接在字符串首添加k+1个数字。 4.如果字符串非全数字,那么结尾的数字一定要被替换成*,否则串无效。 5.遍历整个串,当发现*的个数大于等于数字的个数时,直接把该位置的*与最后的数字进行替换。*/ #include<stdio.h> #include<iostream> #include<string.h> #define N 1005 using namespace std; char str[N]; int s[N]; int main() { int t,a,b,num,ans,pos,k,j,i,n; scanf("%d",&t); while(t--) { scanf("%s",str); n=strlen(str); a=b=0; for(i=0;str[i]!='\0';i++) { if(str[i]=='*') a++; else b++; } if(b==n)//都是数字 { printf("0\n"); continue; } else if(a==n)//都是星号 { printf("%d\n",a+1); continue; } num=0; ans=0; pos=0; if(b<a+1)//数字小于星号,在最前面加数字 { num+=a+1-b; ans+=a+1-b; } k=0; for(i=n-1;i>=0;i--) if(str[i]!='*') s[k++]=i; j=0; for(i=0;str[i]!='\0';i++) { if(str[i]=='*') pos++; else num++; if(pos>=num)//和后面的数字交换位置,使数字尽量靠左,星号尽量靠右 { pos--; swap(str[i],str[s[j++]]); num++; ans++; } } if(str[n-1]!='*') ans++;//最后一个字符不是星号得加1 printf("%d\n",ans); } return 0; }
测试数据:
100 1***1 3 **1 3 1*1*1* 1 1*1*1*1 1 1** 2 1111* 0 1111*1 1 111**1 1 *111* 1 **1111* 2 ***111111* 2 *1**1 3 *1**1* 3 *1111** 1 *1111**1 1 *111**11 1 ***111 3 *** 4 **1 3 *1* 2 *11 1 1* 1 *1 2 1** 2