poj 2269 Friends(表达式求值)
来源:互联网 发布:js将二进制流转成文件 编辑:程序博客网 时间:2024/06/06 01:43
Friends
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 1062 Accepted: 498
Description
You want to plan a big birthday party with your friends. On planning you notice that you have to do a lot of operations with sets of friends. There is one group which consist of Arthur, Biene and Clemens. Then there is a group of friends you know from snowboarding which consists of Daniel, Ernst, Frida and Gustav. If you want to invite them both, the resulting party group consists of g1 + g2 (the result is the union of both groups). Then you can compute the intersection of the two groups g1 * g2, which consists of the empty set. Maybe you want to invite a group g1, but excluding all members of an other group g2, which is written as g1 - g2.
Intersection (*) has precedence over union (+) and set difference (-). All operations are left associative, which means that in A op1 B op2 C you first have to evaluate A op1 B (provided op1 and op2 have equal precedence).
Intersection (*) has precedence over union (+) and set difference (-). All operations are left associative, which means that in A op1 B op2 C you first have to evaluate A op1 B (provided op1 and op2 have equal precedence).
Input
The input consists of one or more lines. Each line contains one expression that you have to evaluate. Expressions are syntactically correct and only consist of the characters:
A line is never longer than 255 characters.
- '{' and '}'
- the elements 'A' to 'Z' meaning friend Arthur to Zora.
- the operations '+', '-' and '*'
- '(' and ')' for grouping operations
- the newline character '\n' marking the end of an expression.
A line is never longer than 255 characters.
Output
Output the resulting set in curly braces '{' and '}', each on a line of its own. Print elements of sets sorted alphabetically.
Sample Input
{ABC}{ABC}+{DEFG}+{Z}+{}{ABE}*{ABCD}{ABCD}-{CZ}{ABC}+{CDE}*{CEZ}({ABC}+{CDE})*{CEZ}
Sample Output
{ABC}{ABCDEFGZ}{AB}{ABD}{ABCE}{CE}
Source
Ulm Local 1999
题意:求表达式的值,+表示并一个集合,-表示去除那个集合的数,*表示取2个集合相同部分,*的优先级比+和-高,同优先级按左结合
题解:和普通的算式求值一样,模拟题
#include<iostream>#include<stdio.h>#include<string.h>#include<stack>using namespace std;char s[100008];int id;struct point{ int c[26]; point(){ memset(c,0,sizeof(c)); } point operator + (const point &p1) { point temp; for(int i=0;i<26;i++) temp.c[i]=c[i]|p1.c[i]; return temp; } point operator - (const point &p1) { point temp; for(int i=0;i<26;i++) temp.c[i]=c[i]&(!p1.c[i]); return temp; } point operator * (const point &p1) { point temp; for(int i=0;i<26;i++) temp.c[i]=c[i]&p1.c[i]; return temp; } void print() { printf("{"); for(int i=0;i<26;i++) if(c[i]) printf("%c",'A'+i); printf("}\n"); }}res;point get(){ point temp; for(id++;s[id]!='}';id++) temp.c[s[id]-'A']=1; return temp;}int getp(char ch){ if(ch=='*') return 2; return 1;}point cal(point p1,char ch,point p2){ if(ch=='+') return p1+p2; else if(ch=='-') return p1-p2; return p1*p2;}point solve(){ point temp,temp2,now; stack<char>op; stack<point>q; if(s[id]=='('){ id++; now=solve(); } else now=get(); q.push(now); for(id++;s[id]!='\0'&&s[id]!=')';id++) { char ch=s[id++]; if(s[id]=='('){ id++; now=solve(); } else now=get(); if(op.empty()){ op.push(ch); q.push(now); } else { char ch2=op.top(); op.pop(); if(getp(ch2)>=getp(ch)) { temp=q.top(); q.pop(); temp2=q.top(); q.pop(); temp=cal(temp2,ch2,temp); q.push(temp); q.push(now); op.push(ch); } else { temp=q.top(); q.pop(); now=cal(temp,ch,now); q.push(now); op.push(ch2); } } } if(!op.empty()) { char ch=op.top(); now=q.top(); q.pop(); temp=q.top(); q.pop(); now=cal(temp,ch,now); } else now=q.top(); return now;}int main(){ while(scanf("%s",s)>0) { id=0; res=solve(); res.print(); } return 0;}
0 0
- poj 2269 Friends(表达式求值)
- Poj2269 Friends (表达式求值)
- POJ-1539(表达式求值)
- ZOJ 1958||POJ 2269 Friends(表达式计算)
- POJ 2269 Friends(模拟)
- poj 1686 表达式求值判断
- poj 1686 Lazy Math Instructor(表达式求值)
- POJ:题号 2269---------Friends问题
- 表达式求值(上)
- 表达式求值(中)
- 表达式求值(下)
- 四则运算(表达式求值)
- 表达式求值(char)
- 表达式求值(nyoj305)
- 表达式求值(一)
- 表达式求值(二)
- 表达式求值(nyoj305)
- 表达式求值(一)
- ZooKeeper典型应用场景一览
- java输入输出流详解
- 为应用指定多个sruts文件
- 【设计模式】迭代器模式
- android textView特效
- poj 2269 Friends(表达式求值)
- 动态及静态的调用Action中的方法
- C#苹果应用开发——第一讲初始Xamarin Xamarin ios 教程 Xamarin跨平台开发
- android原生json包--对象转json
- win7下命令行
- iOS 消息推送原理及实现总结
- 传输线 模型 B 和电阻
- 将NTFS库编译进testdisk/photorec - CYGWIN平台
- spring data jpa Specification 多对一检索