zoukankan      html  css  js  c++  java
  • ural1067 Disk Tree

    Disk Tree

    Time limit: 2.0 second
    Memory limit: 64 MB
    Hacker Bill has accidentally lost all the information from his workstation's hard drive and he has no backup copies of its contents. He does not regret for the loss of the files themselves, but for the very nice and convenient directory structure that he had created and cherished during years of work.
    Fortunately, Bill has several copies of directory listings from his hard drive. Using those listings he was able to recover full paths (like "WINNTSYSTEM32CERTSRVCERTCO~1X86") for some directories. He put all of them in a file by writing each path he has found on a separate line.
    Your task is to write a program that will help Bill to restore his state of the art directory structure by providing nicely formatted directory tree.

    Input

    The first line of the input contains single integer number N (1 ≤ N ≤ 500) that denotes a total number of distinct directory paths. Then N lines with directory paths follow. Each directory path occupies a single line and does not contain any spaces, including leading or trailing ones. No path exceeds 80 characters. Each path is listed once and consists of a number of directory names separated by a back slash ("").
    Each directory name consists of 1 to 8 uppercase letters, numbers, or the special characters from the following list: exclamation mark, number sign, dollar sign, percent sign, ampersand, apostrophe, opening and closing parenthesis, hyphen sign, commercial at, circumflex accent, underscore, grave accent, opening and closing curly bracket, and tilde ("!#$%&'()-@^_`{}~").

    Output

    Write to the output the formatted directory tree. Each directory name shall be listed on its own line preceded by a number of spaces that indicate its depth in the directory hierarchy. The subdirectories shall be listed in lexicographic order immediately after their parent directories preceded by one more space than their parent directory. Top level directories shall have no spaces printed before their names and shall be listed in lexicographic order. See sample below for clarification of the output format.

    Sample

    inputoutput
    7
    WINNTSYSTEM32CONFIG
    GAMES
    WINNTDRIVERS
    HOME
    WINSOFT
    GAMESDRIVERS
    WINNTSYSTEM32CERTSRVCERTCO~1X86
    
    GAMES
     DRIVERS
    HOME
    WIN
     SOFT
    WINNT
     DRIVERS
     SYSTEM32
      CERTSRV
       CERTCO~1
        X86
      CONFIG
    

    分析:trie树;

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <list>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define Lson L, mid, rt<<1
    #define Rson mid+1, R, rt<<1|1
    const int maxn=4e4+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    int n,m,k,t,id;
    set<pair<string,int> >::iterator ptr;
    struct node
    {
        set<pair<string,int> >p;
    }a[maxn];
    char b[maxn],c[maxn];
    void dfs(int now,int t)
    {
        for(set<pair<string,int> >::iterator it=a[now].p.begin();it!=a[now].p.end();it++)
        {
            for(int i=0;i<t;i++)printf(" ");
            printf("%s
    ",it->fi.c_str());
            dfs(it->se,t+1);
        }
    }
    int main()
    {
        int i,j;
        scanf("%d",&n);
        rep(t,1,n)
        {
            scanf("%s",b);
            int len=strlen(b);
            b[len]='\';
            b[len+1]=0;
            j=0;
            int now=0;
            for(i=0;b[i];i++)
            {
                if(b[i]!='\')c[j++]=b[i];
                else
                {
                    c[j]=0;
                    ptr=a[now].p.lower_bound(mp(c,0));
                    if(ptr==a[now].p.end()||strcmp(ptr->fi.c_str(),c)!=0)
                    {
                        a[now].p.insert(mp(c,++id));
                        now=id;
                    }
                    else now=ptr->se;
                    j=0;
                }
            }
        }
        dfs(0,0);
        //system("Pause");
        return 0;
    }
  • 相关阅读:
    System.Web.HtppRuntime.cs
    Code:Tree
    Code-Helper-Web:CacheHelper.cs
    Code-Helper-Web:CookieHelper.cs
    ORACLE数据库常用查询二
    涂抹Oracle笔记2:数据库的连接-启动-关闭
    windows下常用的操作命令及dos命令
    涂抹Oracle笔记1-创建数据库及配置监听程序
    表空间满处理方法
    SQL内连接-外连接join,left join,right join,full join
  • 原文地址:https://www.cnblogs.com/dyzll/p/5853573.html
Copyright © 2011-2022 走看看