ural1067 Disk Tree

本文介绍了一个用于恢复丢失的目录结构的算法。通过分析提供的路径信息,该算法能够重建完整的文件夹层级,并以清晰的格式输出目录树。适用于因硬盘故障等情况导致的目录结构丢失场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 ("!#$%&'()-@^_`{}~").

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
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;
}

转载于:https://www.cnblogs.com/dyzll/p/5853573.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值