1178 File Path (25)

FP.JPG

The figure shows the tree view of directories in Windows File Explorer. When a file is selected, there is a file path shown in the above navigation bar. Now given a tree view of directories, your job is to print the file path for any selected file.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤10^3), which is the total number of directories and files. Then N lines follow, each gives the unique 4-digit ID of a file or a directory, starting from the unique root ID 0000. The format is that the files of depth d will have their IDs indented by d spaces. It is guaranteed that there is no conflict in this tree structure.

Then a positive integer K (≤100) is given, followed by K queries of IDs.

Output Specification:

For each queried ID, print in a line the corresponding path from the root to the file in the format: 0000->ID1->ID2->...->ID. If the ID is not in the tree, print Error: ID is not found. instead.

Sample Input:

14
0000
 1234
  2234
   3234
    4234
    4235
    2333
   5234
   6234
    7234
     9999
  0001
   8234
 0002
4 9999 8234 0002 6666

Sample Output:

0000->1234->2234->6234->7234->9999
0000->1234->0001->8234
0000->0002
Error: 6666 is not found.

 

题目大意:该图显示了Windows文件资源管理器中目录的树状视图。给定一个目录的树状视图,打印选定文件的文件路径。其中根节点编号为0000。对于每个查询的ID,在一行中按格式打印从根到文件的相应路径:0000->ID1->ID2->…->ID。如果ID不在树中,则打印 Error:ID is not found.

分析:1、用一个长度大于10000的数组标记对应编号的节点是否出现过。

2、对于每个节点,需要记录它的父亲节点编号。

3、读入时,用一个栈记录当前文件的目录。如果当前节点深度比栈顶元素的深度深,则说明当前节点在栈顶元素的子目录下;如果当前节点的深度比栈顶元素浅,则说明当前节点不在栈顶元素的子目录,此时一直出栈到栈顶元素深度小于当前节点为止。同时不管是哪种情况,最后都要把当前节点入栈。

4、查询时,首先判断是否出现过。如果出现过,从该节点开始,记录它的父节点编号,最后倒序输出。

#include<algorithm>
#include <iostream>
#include  <cstdlib>
#include  <cstring>
#include   <string>
#include   <vector>
#include   <cstdio>
#include    <queue>
#include    <stack>
#include    <ctime>
#include    <cmath>
#include      <map>
#include      <set>
#define INF 0xffffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
#define db5(x,y,z,r,w) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<", "<<#w<<"="<<(w)<<endl
using namespace std;

typedef struct node
{
    int depth,val,flag,fat;
}node;

int main(void)
{
    #ifdef test
    freopen("in.txt","r",stdin);
    //freopen("in.txt","w",stdout);
    clock_t start=clock();
    #endif //test

    int n;scanf("%d\n",&n);
    node tree[10010];
    for(int i=0;i<10000;++i)
        tree[i].flag=tree[i].fat=-1,tree[i].val=i;
    stack<node>sta;
    for(int i=0;i<n;++i)
    {
        int depth=0,num=0;
        char c;
        for(scanf("%c",&c);c!='\n';)
        {
            if(c==' ')depth++;
            else num=num*10+c-'0';
            scanf("%c",&c);
        }
//        db2(num,depth);
        tree[num].flag=1,tree[num].depth=depth;
        if(num==0)sta.push(tree[0]);
        else
        {
            node p=sta.top();
            if(tree[num].depth>p.depth)
            {
                tree[num].fat=p.val;
                sta.push(tree[num]);
            }
            else if(tree[num].depth<=p.depth)
            {
                while(tree[num].depth<=p.depth)
                {
                    sta.pop();p=sta.top();
                }
                tree[num].fat=p.val;
                sta.push(tree[num]);
            }
        }
    }
    int k;scanf("%d",&k);
    for(int i=0;i<k;++i)
    {
        int id,t=0;scanf("%d",&id);
        if(i>0)printf("\n");
        if(tree[id].flag==-1)
        {
            printf("Error: %04d is not found.",id);
        }
        else
        {
            int cnt=id;
            int ans[10005]={0};
            while(tree[cnt].depth!=0)
            {
                ans[t++]=cnt,cnt=tree[cnt].fat;
            }
            printf("0000");
            for(int i=t-1;i>=0;--i)
                printf("->%04d",ans[i]);
        }
    }

    #ifdef test
    clockid_t end=clock();
    double endtime=(double)(end-start)/CLOCKS_PER_SEC;
    printf("\n\n\n\n\n");
    cout<<"Total time:"<<endtime<<"s"<<endl;        //s为单位
    cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位
    #endif //test
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值