poj 2269 Friends(表达式求值)

Friends
Time Limit: 1000MS Memory Limit: 65536K
Total 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). 

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: 
  • '{' 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


题意:求表达式的值,+表示并一个集合,-表示去除那个集合的数,*表示取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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值