uva12219

Posted 033000-

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了uva12219相关的知识,希望对你有一定的参考价值。

题意见紫书

将子树映射成数字并进行编码即可

具体实现细节可能不太好想,参考lrj代码即可

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<assert.h>
#include<ctime>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<sstream>
#include<stack>
#include<queue>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
#define me(s)  memset(s,0,sizeof(s))
#define _for(i,a,b) for(int i=(a);i<(b);++i)
#define _rep(i,a,b) for(int i=(a);i<=(b);++i)
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define F first
#define S second
#define pf printf
#define sf scanf
#define Di(x) int x;scanf("%d",&x)
#define in(x) inp(x)
#define in2(x,y) inp(x),inp(y)
#define in3(x,y,z) inp(x),inp(y),inp(z)
#define ins(x) scanf("%s",x)
#define ind(x) scanf("%lf",&x)
#define IO ios_base::sync_with_stdio(0);cin.tie(0)
#define READ freopen("C:/Users/ASUS/Desktop/in.txt","r",stdin)
#define WRITE freopen("C:/Users/ASUS/Desktop/out.txt","w",stdout)
template<class T> void inp(T &x) {//读入优化
    char c = getchar(); x = 0;
    for (; (c < 48 || c>57); c = getchar());
    for (; c > 47 && c < 58; c = getchar()) { x = (x << 1) + (x << 3) + c - 48; }
}
typedef pair <int, int> pii;
typedef long long ll;
typedef unsigned long long ull;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-15;
const int maxn=50*1e4+5;
int lch[maxn],rch[maxn];
struct Node{
    string s;
    int l,r,hash;
    bool operator<(const Node &rhs) const {//定义个排序准则
        if(hash!=rhs.hash) return hash<rhs.hash;
        if(l!=rhs.l) return l<rhs.l;
        return r<rhs.r;
    }
}tree[maxn];
char expr[maxn],*p;
int vis[maxn];
int cnt;//分配节点
int cas;
map<Node,int> dict;
int build(){
    int id=++cnt;
    Node &u=tree[id];
    u.l=u.r=-1;
    u.s="";u.hash=0;
    while(isalpha(*p)){//提取节点名字
        u.hash=u.hash*27+(*p)-a+1;
        u.s.pb(*p);
        p++;
    }
    if(*p==(){
        p++;//跳过左括号
        u.l=build();
        p++;//跳过逗号
        u.r=build();
        p++;//跳过又括号
    }
    if(dict.count(u)!=0) {
        --cnt;
        return dict[u];
    }
    return dict[u]=id;
}
void print(int rt){
    if(vis[rt]==cas) printf("%d",rt);
    else{
        vis[rt]=cas;   
        printf("%s",tree[rt].s.c_str());
        if(tree[rt].l!=-1){
            putchar(();
            print(tree[rt].l);
            putchar(,);
            print(tree[rt].r);
            putchar());
        }
    }
}
int main(){
    //READ;
    //WRITE;
    Di(T);me(vis);
    for(cas=1;cas<=T;cas++){
        ins(expr);
        p=expr;
        cnt=0;
        dict.clear();
        print(build());
        puts("");
    }
}

 

以上是关于uva12219的主要内容,如果未能解决你的问题,请参考以下文章

uva 12219 公共表达式消除

uva-12219

UVA12219

UVa12219 Common Subexpression Elimination (表达式树)

UVa 12219 - Common Subexpression Elimination

表达式树(公共表达式消除 uva 12219)