栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 1785 Binary Search Heap C...

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 1785 Binary Search Heap C...

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>using namespace std;#define maxn 100005struct Node{    char c[100];    int v;    int l, r, fa;} tree[maxn];int n;bool operator<(const Node &a, const Node &b){    return strcmp(a.c, b.c) <0;}void input(){    for (int i =1; i <= n; i++)        scanf("%*[ ]%[^/]/%d", tree[i].c, &tree[i].v);}void ins(int i){    int j = i -1;    while (tree[j].v < tree[i].v)        j = tree[j].fa;    tree[i].l = tree[j].r;    tree[tree[i].l].fa = i;    tree[j].r = i;    tree[i].fa = j;}void dfs(int i){    if (!i)        return;    printf("(");    dfs(tree[i].l);    printf("%s/%d", tree[i].c, tree[i].v);    dfs(tree[i].r);    printf(")");}int main(){        while (scanf("%d", &n), n)    {        input();        tree[0].l = tree[0].r = tree[0].fa =0;        tree[0].v =0x3f3f3f3f;        sort(tree +1, tree + n +1);        for (int i =1; i <= n; i++)        { tree[i].l = tree[i].r = tree[i].fa =0; ins(i);        }        dfs(tree[0].r);        printf("n");    }    return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/378111.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号