PAT甲级1178FilePath

文章原文:

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 (≤103), 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.

题解:

根据文章题意可以知道就是要根据子文件去寻找从根文件到子文件的路径,由于一开始给出的是最终要查找的子文件,我们可以通过子文件逐层向上寻找他的父文件,然后再用stack来保存路径,最后根据stack进行输出即可。然后如何去寻找他的父文件是关键,根据观察可以知道,子文件一般紧邻父文件(即就近寻找父文件),且子文件比父文件多一个空格,我们可以根据这几点要素来对父文件进行寻找。简单的可以直接使用字符串的长度来进行判断,这个时候我们读取就不能省略空格,可以使用getline(cin,string)来对string进行赋值,具体代码如下:

#include<bits/stdc++.h>
using namespace std;
vector<string>v1;
vector<string>v2;
stack<string>s1;
void find_fathers(string s){
    int pos;
    for(int i=1;i<(int)v2.size();i++){
        if(v2[i]==s){
            pos=i;
            break;
        }
    }
    s1.push(s);
    int q=pos-1;
    while(pos>1){     
        // cout<<"v1[pos]"<<v1[pos]<<endl;
        // cout<<"v1[q]"<<v1[q]<<endl;
        if(v1[pos].length()>v1[q].length()){
            s1.push(v2[q]);
            pos=q;}
        q--;}
    while(!empty(s1)){
        string temps=s1.top();
        s1.pop();
        if(empty(s1))
            cout<<temps<<endl;
        else
            cout<<temps<<"->";
    }}
int main(){
    int nums;
    cin>>nums;
    for(int i=0;i<nums+1;i++){
        string tem;
        string tem2;
        getline(cin,tem);
        if(tem==" ")
            continue;
        v1.push_back(tem);
        for(int j=0;j<(int)tem.length();j++){
            if(tem[j]!=' ')
                tem2+=tem[j];
        }
        v2.push_back(tem2);
    }
    int target;
    cin>>target;
    for (int i=0;i<target;i++){
        string tar;
        cin>>tar;
        if(find(v2.begin(),v2.end(),tar)==v2.end())
            cout<<"Error: "<<tar<<" is not found."<<endl;
        else
            find_fathers(tar);
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值