ZOJ 1014 OPERAND
来源:互联网 发布:沪深300指数基金 知乎 编辑:程序博客网 时间:2024/06/09 16:08
Professor Maple teaches mathematics in a university. He have invented a function for the purpose of obtaining the operands from an expression. The function named op(i,e) can be described as follows:
The expression e may be divided into sub-expression(s) by the operator, which has the lowest priority in the expression. For example, the expression "a*b+b*c+c*d" should be divided into three sub-expressions "a*b", "b*c" and "c*d", because the operator "+" has the lowest priority. The purpose of this function is to extract the ith sub-expression as the result. So, in the example above, op(2,e)=b*c.
If we regard the sub-expression as the main expression, it might be divided again and again. Obviously, the dividing process is recursive. As you see, the following example is much more complex:
Let p:=a^b*c+(d*c)^f*z+b
op(1,op(1,op(2,p)))=(d*c)
op(1,op(1,op(1,op(2,p))))=d*c
op(2,op(2,p))=z
op(3,p)=b
op(1,op(3,p))=b
Professor Maple is so lazy that he would leave the work to computer rather than do it himself, when the expression is long and complicated. Of course, without your program, the computer won't work out the result automatically.
Input
The input file contains several test cases. The last test case in the input file is followed by a line containing a symbol "*", indicating the end of the input data. Each test case consists of two parts. The first part describes the expression, while the second part contains several questions, which should be calculated according to the expression.
The first line of each test case contains an expression consists of the expression name, ":=" and the content of the expression. The expression name is a lowercase. And the content is composed by lowercases and operators "+", "(", ")", "*" and "^". For example, here is a valid expression, p:=a^b*c+(d*c)^f*z+b. Among those operators, "(" and ")" have the highest priority. The operator "^" has a lower priority, and then "*". The priority of the operator "+" is the lowest.
The second line of each test case contains an integer n indicating n questions based on the above expression. This is followed by n lines. Each of them contains the description of one question, which consists of integers. For example, the question with three integers "2 1 1" describes the function op(1,op(1,op(2,e))). To compute this function, we have to keep to the following sequence: First, according to the first integer 2, divide the expression and extract the 2nd sub-expression. Then, according to the second integer 1, divide the sub-expression and extract the 1st one. Finally, according to the third integer 1, divide the outcome again, and extract the result.
Output
For each test case, display the expression name and a colon on the first line. Then display the result of each question on a line. The layout of the output is shown in the sample output.
You may assume that all expressions and functions are always valid.
Display a blank line between test cases.
Sample Input
p:=a^b*c+(d*c)^f*z+b
4
2 1 1
2 2
3
3 1
a:=(x+y)
3
1
1 2
1 2 1
*
Output for the Sample Input
Expression p:
op(1,op(1,op(2,p)))=(d*c)
op(2,op(2,p))=z
op(3,p)=b
op(1,op(3,p))=b
Expression a:
op(1,a)=x+y
op(2,op(1,a))=y
op(1,op(2,op(1,a)))=y
#include<cstdio>
#include<list>
#include<iostream>
#include<algorithm>
using namespace std;
char op[4]="+*^";
string on(int n,string s){
int coun;
int bckt;
bool fnd;
string ans;
for(int i=0;i<3;i++){
coun=1;
bckt=0;
fnd=0;
ans="";
for(int j=0;j<s.length();j++){
ans+=s[j];
if(s[j]=='('){
bckt++;
continue;
}
else if(s[j]==')'){
bckt--;
continue;
}
else if(bckt)continue;
else if(s[j]!=op[i])continue;
else{
if(coun==n)return ans.substr(0,ans.length()-1);
else {
fnd=1;
ans="";
coun++;
}
}
}
if(fnd)
return ans;
}
if(s[0]=='(')return s.substr(1,s.length()-2);
else return s;
}
int main(){
int coun;
string src;
char ename;
bool lf=1;
list<int>question;
while(cin>>src&&src!="*"){
if(lf)lf=0;
else puts("");
ename=src[0];
src=src.substr(3);
cout<<"Expression "<<ename<<":"<<endl;
cin>>coun;
cin.ignore();
while(coun--){
int n;
string opp=src;
question.clear();
while(cin.peek()!='\n'){
cin>>n;
question.push_back(n);
opp=on(n,opp);
}
cin.ignore();
int bkc=question.size();
while(!question.empty()){
n=question.back();
question.pop_back();
cout<<"op("<<n<<",";
}
cout<<ename;
for(int i=1;i<=bkc;i++)
cout<<")";
cout<<"="<<opp<<endl;
}
}
return 0;
}
- ZOJ 1014 Operand
- ZOJ 1014 OPERAND
- ZOJ 1014 Operand
- JOJ1037:Operand
- zoj 1014
- zoj 1014
- invalid 'in' operand a
- dirname: missing operand 问题解决
- Fatal error: Unsupported operand types108
- error C2443: operand size conflict
- ‘asm’ operand has impossible constraints
- 谈一谈c++中的unevaluated operand
- jvm Local Variables ,Operand Stacks
- zoj 1014 ---未解决
- ZOJ
- ZOJ
- ZOJ
- ZOJ
- Vim 启动错误Caught deadly signal SEGV
- java.lang.OutOfMemoryError: Java heap space 解决方法
- linux 信号 中断的系统调用
- php设计模式:值对象模式
- C#中的foreach语句与枚举器接口(IEnumerator)及其泛型 相关问题
- ZOJ 1014 OPERAND
- IOS BezierPath 贝塞尔曲线画图详解
- 238,安装mysql出现Access denied for user 'root'@'localhost' (using password: YES)的问题
- ubuntu 的 document viewer evince 发生 Could not open X display 错误的处理
- Android 四大组件 —— 广播(广播机制解析)
- Interview Notes
- CAS单点登陆的原理
- poj2492 A Bug's Life(并查集)
- Hadoop—archive