uva-12219

https://vjudge.net/problem/UVA-12219

思路:把每一个子树用一个数代替放到map里,这样查一棵子树是否出现就是快多了。递归打印答案即可。

#include<bits/stdc++.h>
#define _for(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
typedef long long ll;
const int mod =1e6+7;
double esp=1e-6;
int INF =0x3f3f3f3f;
const int inf = 1<<28;
const int MAXN=60000;
char s[MAXN*5],*p;
int T,n,m,cnt,kase;
struct node
{
    string s;
    int hash,l,r;
    bool operator <(const node&p)const//map比较的条件
    {
        if(hash!=p.hash)return hash<p.hash;
        if(l!=p.l)return l<p.l;
        return r<p.r;
    }
}nodes[MAXN];
map<node,int> mp;
int solve()
{
    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+=*p;
        ++p;
    }
    if(p&&(*p)=='(')
    {
        p++;
        u.l=solve();
        p++;
        u.r=solve();
        p++;
    }
    if(mp.count(u))//如果map里有,就直接返回这个子树的id
    {
        cnt--;
        return mp[u];
    }
    return mp[u]=id;
}
int ex[MAXN];
void print(int v)//递归打印
{
    if(ex[v]!=kase)
    {
        ex[v]=kase;
        cout<<nodes[v].s;
        if(nodes[v].l!=-1)
        {
            printf("(");
            print(nodes[v].l);
            printf(",");
            print(nodes[v].r);
            printf(")");
        }
    }
    else
    {
        printf("%d",v);
    }
}
int main()
{
    scanf("%d",&T);
    for(kase=1;kase<=T;kase++)
    {
        mp.clear();
        scanf("%s",s);
        p=s;
        cnt=1;
        int tmp=solve();
        print(tmp);
        printf("\n");
    }
    return 0;
}

 

猜你喜欢

转载自www.cnblogs.com/kayiko/p/12324292.html