admin管理员组

文章数量:1516870

题目简介

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 (≤ 10310^3 1 0 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.

题解

#include<bits/stdc++.h>voidStringSplit(std::string str,constchar split, std::vector<std::string>& result){
    std::istringstream ss(str);
    std::string token;while(std::getline(ss, token, split)){
        result.push_back(token);}}
std::string del_space(std::string str){int index =0;for(constchar c : str){if(c ==' ') index++;elsebreak;}return str.substr(index);}intmain(){int N;
    std::cin >> N;
    std::string s, root;
    std::set<std::string> set_file;
    std::vector<std::string> files;
    std::cin >> root;
    std::cin.ignore();
    files.push_back(root);for(int i =1; i < N; i++){
        std::getline(std::cin, s);
        std::string str =del_space(s);
        set_file.insert(str);
        files.push_back(s);}int K;
    std::cin >> K;
    std::vector<std::string>query(K);for(int i =0; i < K; i++){
        std::cin >> query[i];}for(int i =0; i < K; i++){
        std::vector<std::string> result;
        std::string query_file = query[i];if(query_file !="0000"&& set_file.find(query_file)== set_file.end()){
            std::cout <<"Error: "<< query_file <<" is not found."<< std::endl;}elseif(query_file =="0000"){
            std::cout << query_file << std::endl;}else{int pos =0;for(int j =0; j < N; j++){
                std::string s =del_space(files[j]);if(s == query_file){
                    pos = j;break;}}
            result.push_back(query_file);int cur_length = files[pos].size();for(int k = pos -1; k >=0; k--){if(files[k].size()< cur_length){
                    cur_length = files[k].size();
                    std::string s =del_space(files[k]);
                    result.push_back(s);}}if(!result.empty()){
                std::reverse(result.begin(), result.end());for(int i =0; i < result.size(); i++){if(i != result.size()-1){
                        std::cout << result[i]<<"->";}else{
                        std::cout << result[i]<< std::endl;}}}}}return0;}

本文标签: 环境中快编程轻松掌握