大疆笔试:N叉树搜索文件

不知道为啥,自己代码的输出总是缺了中间一部分,有没有佬帮忙看看问题出在哪:
正确输出:
/root/folder2/file4.txt
/root/folder4/
代码实际输出:
/root/file4.txt
/root/folder4/

输入:
```
4
11
root/
-folder1/
--file1.txt
--file2.txt
-folder2/
--file3.txt
--file4.txt
-folder3/
--file5.txt
-folder4/
--file6.txt
```
关键代码:
```
vector tmp;
vector result;
void aa(Tree* node, string& key) {
tmp.push_back(node->val);
if (node->val.find(key) != string::npos) {
    string str = "/";
    for (auto i : tmp) str += i;
    result.push_back(str);
  }
if (node->child.empty()) {
    tmp.pop_back();
    return;
  }
for (auto i : node->child) {
    aa(i, key);
  }
  tmp.pop_back();
}
```
全部评论
贴个大佬的解答,一眼的差距
3
送花
回复
分享
发布于 2023-08-13 21:24 湖北
#include<bits/stdc++.h> using namespace std; struct Tree { string val; vector<Tree*> child; Tree(string tmp, vector<Tree*> vec) : val(tmp), child(vec) {}; }; int get(string&amp; tmp) { int count = 0; for (auto i : tmp) { if (i == '-') count++; else return count; } return count; } vector<string> tmp; vector<string> result; void aa(Tree* node, string&amp; key) { tmp.push_back(node->val); if (node->val.find(key) != string::npos) { string str = "/"; for (auto i : tmp) str += i; result.push_back(str); } if (node->child.empty()) { tmp.pop_back(); return; } for (auto i : node->child) { aa(i, key); } tmp.pop_back(); }
点赞
送花
回复
分享
发布于 2023-08-13 21:04 湖北
滴滴
校招火热招聘中
官网直投
int main() { std::string keyword; std::cin >> keyword; // 读取关键字 int count = 0; std::cin >> count; std::vector<std::string> contents; // 每行的数据 for(int i = 0;i<count;++i){ std::string tmp; std::cin >> tmp; contents.push_back(tmp); } // < -数量,node> std::vector<pair<int, Tree*>> vec; for (int i = 0; i < contents.size(); i++) { int tmp = get(contents[i]); string tmp2 = contents[i].substr(tmp); Tree* node = new Tree(tmp2, vector<Tree*>()); vec.push_back({tmp, node}); } // 构建N叉树 deque<int> deq; deq.push_back(0); for (int i = 1; i < contents.size(); i++) { while (get(contents[i]) <= vec[deq.front()].first) { deq.pop_back(); } Tree* node = vec[deq.front()].second; node->child.push_back(vec[i].second); deq.push_back(i); } aa(vec[0].second, keyword); for (auto i : result) { cout << i << endl; } return 0; }
点赞
送花
回复
分享
发布于 2023-08-13 21:04 湖北
我的是这#include <fstream> (31579)#include <iostream> #include <numeric> (30195)#include <sstream> #include <string> (30191)#include <vector> using namespace std; int finded = false; struct Node {     string        val;     Node*         father;     vector<Node*> next{};     Node(string val)         : val(val), father(nullptr) {} }; void deal(string line, Node* root) {     if (line.find('-') == string::npos) {         Node* one = new Node(line);         root->next.emplace_back(one);         one->father = root;     } else {         size_t start = line.find('-');         string lline = line.substr(start + 1, line.size() - start - 1);         root         = root->next.back();         deal(lline, root);     } } void pri(Node* root, string str, const string& keyword) {     if (root->val.find(keyword) != string::npos) {         cout << str + root->val << endl;     }     for (const auto& r : root->next) {         pri(r, str + root->val, keyword);     } } int main() {     std::string keyword;     std::cin >> keyword;  // 读取关键字     int count = 0;     std::cin >> count;     Node *vroot = new Node("/"), *cur = vroot;     for (int i = 0; i < count; ++i) {         std::string line;         std::cin >> line;         cur = vroot;         deal(line, cur);     }     // for(const auto& r : vroot->next[0]->next[0]->next) {     //     cout << r->val << endl;     // }     pri(vroot, "", keyword);     return 0; }
点赞
送花
回复
分享
发布于 2023-08-13 21:20 安徽
写完忘调用dfs的函数了,算求了,时间太紧
点赞
送花
回复
分享
发布于 2023-08-13 21:57 广东
构建N叉树的时候为什么用队列?不是应该用栈吗,输入是递归形式的树
点赞
送花
回复
分享
发布于 2023-08-13 23:24 北京

相关推荐

点赞 6 评论
分享
牛客网
牛客企业服务