标签:字符串 2014acm牡丹江区域赛 算法 acm
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5383
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:
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 *".
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.
For each test case, output the minimal number of operations to make the given string able to represent a valid RPN.
3 1*1 11*234** *
1 0 2
最近忙着各种事情,好久没写博客了。。。
这道题是牡丹H题,当时一直纠结一道概率DP。。。这题并没有深入的想,到比赛后才搞出来。。。
首先我们可以先对字符串进行补全,因为数字数目必须是’*‘数目+1。之后呢,我们发现对于‘*’,我们
其实只要把它尽可能的丢到后面去就完了。。然后从头到尾扫一遍。
不过有些特殊情况记得要小心。。
#include<iostream> #include<cstring> #include<cstdio> #include<cmath> #include<algorithm> #include<string> #include<map> #include<set> #include<vector> #include<bitset> #include<cstdlib> #define CLR(A) memset(A,0,sizeof(A)) using namespace std; int main(){ int T; while(~scanf("%d",&T)){ while(T--){ string str; cin>>str; int cnum=0,cop=0,ans=0; for(int i=0;str[i];i++){ if(str[i]>='0'&&str[i]<='9') cnum++; else cop++; } if(cnum<cop+1) { ans+=(cop+1-cnum); cnum=cop+1-cnum; } else cnum=0; if(cop==0){ cout<<"0"<<endl; continue; } int len=str.size(); int last=len-1; bool flag=0; while(last>=0&&!(str[last]>='0'&&str[last]<='9')) last--; for(int i=0;str[i];i++){ if(i>=last) break; if(str[i]>='0'&&str[i]<='9'){ cnum++;} else{ if(cnum<2){ ans++; cnum++; last--; flag=1; while(last>=0&&!(str[last]>='0'&&str[last]<='9')) last--; } else cnum--; } } if(flag==0&&str[len-1]!='*') ans++; cout<<ans<<endl; } } return 0; }
ZOJ 3829 Known Notation (2014牡丹江H题)
标签:字符串 2014acm牡丹江区域赛 算法 acm
原文地址:http://blog.csdn.net/asdfghjkl1993/article/details/40321245