UVA 12219 Common Subexpression Elimination(STL)

题意就是求最小的表达式树,也就是把相同的表达式子树给替换成最前面相同的编号。

做法:因为需要一一比较,所以把树给编号,方法是用根节点的字符串加左子树编号以及右子树编号。这样经过一次dfs就可以知道哪些是有重复的了。他要输出的编号可以用一个全局变量x,过程中遇到新的字符串就加加,如果这个树与子树是前面出现过的,那么就把x--。结构体里不仅要保存根节点字符串,左右子树编号,还要保存第一次出现的位置,这样才能满足输出的条件。

AC代码:

#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<ctype.h>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdlib>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<ctime>
#include<string.h>
#include<string>
#include<sstream>
#include<bitset>
using namespace std;
#define ll __int64
#define ull unsigned long long
#define eps 1e-8
#define NMAX 1000000000
#define MOD 1000000
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1)
#define ALL(x) x.begin(), x.end()
#define INS(x) inserter(x, x.end())
template<class T>
inline void scan_d(T &ret)
{
    char c;
    int flag = 0;
    ret=0;
    while(((c=getchar())<'0'||c>'9')&&c!='-');
    if(c == '-')
    {
        flag = 1;
        c = getchar();
    }
    while(c>='0'&&c<='9') ret=ret*10+(c-'0'),c=getchar();
    if(flag) ret = -ret;
}
template<class T> inline T Max(T a, T b){ return a > b ? a : b;}
template<class T> inline T Min(T a, T b){ return a < b ? a : b;}
const int maxn = 50000+10;
char ch[maxn*10];
int lch[maxn],rch[maxn],nct,pos;
char op[maxn][6];
struct node
{
    char s[6];
    int l,r,wei;
    bool operator < (const node &t) const
    {
        int p = strcmp(s,t.s);
        if(p == 0) return l == t.l ? r < t.r : l < t.l;
        return p > 0 ? 0 : 1;
    }
};
map<node, int>mp;
vector<node>v;
int flag[maxn],ji;
bool pp;
int ID(node x)
{
    if(mp.find(x) != mp.end())
    {
        pp = 1;
        return mp[x];
    }
    v.push_back(x);
    return mp[x] = v.size()-1;
}

node make_node(char *s, int l, int r, int wei)
{
    node t; strcpy(t.s,s); t.l = l; t.r = r; t.wei = wei; return t;
}

int build(int u)
{
    int i = 0;
    while(ch[pos] >= 'a' && ch[pos] <= 'z')
    {
        op[u][i++] = ch[pos++];
    }
    int qi = ++ji;
    op[u][i] = '\0';
//    cout<<op[u]<<" "<<pos<<endl;
    if(ch[pos] == '(')
    {
        pos++;
        lch[u] = ++nct;
        int ID1 = build(lch[u]);
        pos++;
        rch[u] = ++nct;
        int ID2 = build(rch[u]);
        pos++;
        pp = 0;
        int ret = ID(make_node(op[u],ID1,ID2,qi));
        if(pp)
        {
            flag[u] = v[ret].wei;
            ji--;
        }
        return ret;
    }
    pp = 0;
    int ret = ID(make_node(op[u],-1,-1,qi));
    if(pp)
    {
        flag[u] = v[ret].wei;
        ji--;
    }
//    if(pos == 21) cout<<op[u]<<" "<<flag[u]<<" "<<qi<<endl;
    return ret;
}

void dfs(int u)
{
    if(flag[u])
    {
        printf("%d",flag[u]);
        return;
    }
    printf("%s",op[u]);
    if(lch[u] != 0)
    {
        printf("(");
        dfs(lch[u]);
        printf(",");
        dfs(rch[u]);
        printf(")");
    }
}

int main()
{
#ifdef GLQ
    freopen("input.txt","r",stdin);
//    freopen("o.txt","w",stdout);
#endif
    int cas;
    scanf("%d\n",&cas);
    while(cas--)
    {
        scanf("%s",ch);
        nct = 1; pos = ji = 0;
        mp.clear();
        v.clear();
        memset(flag,0,sizeof(flag));
        memset(lch,0,sizeof(lch));
        memset(rch,0,sizeof(rch));
        build(1);
        dfs(1);
        printf("\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值