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 "WINNT\SYSTEM32\CERTSRV\CERTCO~1\X86") 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 ("!#$%&‘()[email protected]^_`{}~").

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

input output
7
WINNT\SYSTEM32\CONFIG
GAMES
WINNT\DRIVERS
HOME
WIN\SOFT
GAMES\DRIVERS
WINNT\SYSTEM32\CERTSRV\CERTCO~1\X86
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\n",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;
}
时间: 2024-10-09 02:19:05

Disk Tree的相关文章

uva 1556 - Disk Tree(字典树)

题目连接:uva 1556 - Disk Tree 题目大意:给出N个目录关系,然后按照字典序输出整个文件目录. 解题思路:以每个目录名作为字符建立一个字典树即可,每个节点的关系可以用map优化. #include <cstdio> #include <cstring> #include <map> #include <string> #include <iostream> #include <algorithm> using nam

UVA 1556 - Disk Tree(Trie)

UVA 1556 - Disk Tree 题目链接 题意:给定一些字符串,表示目录,要求输出整体目录的结构 思路:跟Trie树差不多,只不过是每个结点存放的是一个字符串,利用map映射即可 代码: #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <string> #include <map> #include &l

timus 1067 Disk Tree【超时代码】

题目的描述链接timus 1067 这个题目困扰了很久,后面理清思路了,解法也很明确,结果是超时! 在写代码时也收获了一些: 在把思路理好后去写代码会更顺利点,边想思路边写代码效率比较低! 先把代码放在这里,超时的代码不好说什么,以后还可以学到一些东西!第一次用new 和delete,比较怂!(2.046s>2.0s TLE) #include<iostream> #include<algorithm> using namespace std; struct Disk { i

UVa 1556 Disk Tree

方法: Trie 其实就是建树,node里存文件名.因为最后同目录下的文件要按照文件名顺序输出,所以储存child pointer的时候用了map,自带排序.下面的code是动态分配内存,然而用数组也可以,参见trie template. Code: 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <stri

【HDOJ】1504 Disk Tree

文件可以重名.先按字典序将路径排序,再过滤掉公共前缀.其中的问题是'\'的ASCII比[A-Z0-9]大,将它替换为空格.否则字典序有问题. 1 /* 1504 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #include <vector

Linux平台下源码安装mysql多实例数据库

Linux平台下源码安装mysql多实例数据库[[email protected] ~]# netstat -tlunp | grep 330tcp6 0 0 :::3306 :::* LISTEN 6191/mysqld [[email protected] ~]# ss -tlunp | grep 330tcp LISTEN 0 80 :::3306 :::* users:(("mysqld",pid=6191,fd=10)) [[email protected] ~]# syst

Hierarchical Tree Traversal in Graphics Pipeline Stages

BACKGROUND Many algorithms on a graphics processing unit (GPU) may benefit from doing a query in a hierarchical tree structure (including quad-trees, oct-trees, kd-trees, R-trees, and so forth). However, these trees can be very deep, whereby traversi

VMSS上用Managed Disk和Data Disk进行自动扩展(2)

10. 到目前为止,基本的模板配置已经完成,接下来我们使用Azure CLI 2.0来创建虚拟机自动扩展集合: az group create --name linuxvmssmanaged --location 'China North' az group deployment create --name mylinuxdeployment --resource-group linuxvmssmanaged --template-file vmsslinuxmanaged.json --par

CBO之B*Tree Index Range Scan - IRS算法

转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/44262353 ************************************************************  二.CBO之B*Tree Index Range Scan - IRS算法************************************************************* 1.在表gyj_t1建索引 SQL> create i