zoukankan      html  css  js  c++  java
  • HDU 1504 Disk Tree

    转载请注明出处:http://blog.csdn.net/a1dark

    分析:查了一下这题、发现网上没有什么关于这道题的解题报告、其实题目意思挺好懂的、就是给你一些文件的目录结构、然后让你把它们组合在一起、然后按照目录结构输出、注意是字典序、这道题是一个模拟、主要是对结构体和指针的掌握、使用嵌套结构体模拟文件的同级和子级文件、然后进行读取、插入、查询等操作、代码如下(0ms):

    #include<stdio.h>
    #include<string.h>
    struct node{  
        node *child;
        node *brother;
        char key[10]; 
    };
    int n,m;
    node *root;
    void getname(char *str,char *key,int &j)
    {
        int i;
        for(i=0;str[j]!=''&&str[j]!='\';i++,j++)
            key[i]=str[j];
        key[i]='';
    }
    node *insert(node *parent,char *key)
    {
        node *p,*q,*t;
        if(!parent->child||strcmp(parent->child->key,key)>0)
        {
            t=new node;
            strcpy(t->key,key);
            t->child=NULL;
            t->brother=parent->child;
            parent->child=t;
            return t;
        }
        if(strcmp(parent->child->key,key)==0)
            return parent->child;
        for(p=parent->child,q=p->brother;q&&strcmp(q->key,key)<0;p=q,q=q->brother);
        if(!q||strcmp(q->key,key)>0)
        {
            t=new node;
            strcpy(t->key,key);
            t->brother=p->brother;
            p->brother=t;
            t->child=NULL;
            return t;
        }
        return q;
    }
    void read()
    {
        char str[90],key[9];
        int i,cur;
        node *p;
        root=new node;
        root->child=NULL;
        scanf("%d",&n);
        for(i=m=0;i<n;i++)
        {
            cur=0;
            scanf("%s",str);
            getname(str,key,cur);
            for(p=insert(root,key);str[cur]!='';)
            {
                getname(str,key,++cur);
                p=insert(p,key);
            }
        }
    }
    void find(node *p,int k)
    {
        int i;
        for(;p;p=p->brother)
        {
            for(i=0;i<k;i++)
                putchar(' ');
            puts(p->key);
            if(p->child)
                find(p->child,k+1);
        }
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            read();
            find(root->child,0);
            if(t)
                printf("
    ");
        }
        return 0;
    }


  • 相关阅读:
    input只允许输入正整数
    CSS如何作小于1PX的边
    时间戳的处理
    图片转base64上传,视频同理。
    APIcloud微信支付和支付宝支付(方案2,主要在后台进行)
    H5滑条(input type=range)
    checkbox/radio 样式修改
    APIcloud制作APP 微信支付与支付宝支付
    JS获取鼠标左(右)滑事件
    DOM(Document object madle) 文档对象模型: 元素节点 文本节点 属性节点
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3285794.html
Copyright © 2011-2022 走看看