栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 2269 Friends

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 2269 Friends

#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]!=''&&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;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/373709.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号