• 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;
    }
  • 相关阅读:
    Matlab矩阵填充--Matlab interp2
    Object::connect: No such slot (QT槽丢失问题)
    QT显示框架嵌入Vs控制台工程
    SLAM: 关于Orb_SLAM的使用小综述
    SLAM: Orb_SLAM中的ORB特征
    Ubuntu安装中文语言包
    装X数学:高雅的数学表示
    图像描述:各种维度图像的逻辑描述形式
    Python__configparser模块
    Python__xml模块
  • 原文地址:https://www.cnblogs.com/dyzll/p/5853573.html
Copyright © 2020-2023  润新知