poj 1785
来源:互联网 发布:单片机控制8个led灯 编辑:程序博客网 时间:2024/06/07 16:36
#include<cstdio>#include<cstdlib>#include<iostream>#include<algorithm>#include<cstring>using namespace std;const int inf=~0U>>1;struct ps{ char s[11]; int w; int l,r; friend bool operator <(ps p,ps q){return strcmp(p.s,q.s)<=0;}}d[51111];int n;char a[10];int ele[55555],top;void dfs(int x){ if(x) { putchar('('); dfs(d[x].l); printf("%s/%d",d[x].s,d[x].w); dfs(d[x].r); putchar(')'); }}void dfs(){ dfs(ele[1]); puts("");}int main(){ while(scanf("%d",&n)==1&&n) { for(int i=1;i<=n;++i) { getchar(); int p; for(int j=0;;++j) { char c=getchar(); if(c=='/') {a[j]='\0'; break;} a[j]=c; } scanf("%d",&p); strcpy(d[i].s,a); d[i].w=p; d[i].l=d[i].r=0; } sort(d+1,d+1+n); top=0; for(int i=1;i<=n;++i) { if(top==0) { top++; ele[top]=i; } else { int pre=0; while(d[ele[top]].w<d[i].w&&top) pre=ele[top],--top; if(top==0); else d[ele[top]].r=i; if(pre) d[i].l=pre; ele[++top]=i; } } dfs(); } return 0;}