现在的位置: 首页 > 综合 > 正文

poj 2269 Friends(表达式求值)

2014年09月29日 ⁄ 综合 ⁄ 共 2949字 ⁄ 字号 评论关闭
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;
}

抱歉!评论已关闭.