12219 - Common Subexpression Elimination(表达式树)

草草刷了一下暴力,开始转战图论了。   这是第一道例题,讲解了一种实用而神奇的树状结构:表达式树 。虽然打比赛从来没见过,但是我练这个本来也不只是为了比赛 , 重要的是ACM本身带给我的乐趣 。

该题的一个很巧妙的做法是将每一个结点用一个三元组来表示,然后映射到map中以去重 。 其中三元组中有一个string , 我们可以用hash来处理这个string 。

因为string最大长度为4, 所以我们可以这样处理 : int hash = 0;    hash = hash * 27 + s[i] - '0' + 1; 

将hash每次乘以27 是因为字符串只有小写字母,这样做可以完美的去重 ,保证了哈希表不会重复 。 

细节参见代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 60000;
int T,n,m,kase,cnt,done[maxn];
char s[maxn*5],*p;
struct node {
    string s;
    int hash,l,r;
    bool operator < (const node& u) const {
        if(hash != u.hash) return hash < u.hash;
        if(l != u.l) return l < u.l;
        return r < u.r;
    }
}nodes[maxn];
map<node,int> dict;
int parse() {
    int id = cnt++;
    node& u = nodes[id];
    u.l = u.r = -1 ;
    u.s = "";
    u.hash = 0;
    while(isalpha(*p)) {
        u.hash = u.hash * 27 + *p - 'a' + 1;
        u.s.push_back(*p);
        ++p;
    }
    if(*p == '(') {
        p++; u.l = parse(); p++; u.r = parse(); p++;
    }
    if(dict.count(u)) {
        id--; cnt--;
        return dict[u];
    }
    return dict[u] = id;
}
void print(int v) {
    if(done[v] == kase) printf("%d",v+1);
    else {
        done[v] = kase;
        printf("%s",nodes[v].s.c_str());
        if(nodes[v].l != -1) {
            printf("(");
            print(nodes[v].l); //递归求得结点值并打印 。
            printf(",");
            print(nodes[v].r);
            printf(")");
        }

    }
}
int main() {
    scanf("%d",&T);
    for(kase = 1; kase <= T; ++kase) {
        dict.clear();
        cnt = 0;
        scanf("%s",s);
        p = s;
        print(parse());
        printf("\n");
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值