https://pintia.cn/problem-sets/15/problems/857
出bug的地方在第59行insert函式部分,應該是指標出的問題,求大佬在線講解

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <unordered_map>
#include <set>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <list>
#include <iomanip>
//#include <sstream>
//#include <regex>
//#include <functional>
using namespace std;
struct Node {
string data;
set<Node>link;
set<string>file;
bool operator<(const Node& n)const {
return data < n.data;
}
};
//void insert(Node& root, string str) {
// if (str.empty())
// return;
// Node n;
// int index = str.find('\\');
// cout << " find_index:" << index << endl;
// if (index != -1) {
// string s = str.substr(0, index);
// n.data = s;
// if (!root.link.insert(n).second)
// for (auto& it : root.link)
// if (it.data == n.data) {
// n = it;
// break;
// }
// str = str.substr(index + 1);
// insert(n, str);
// }
// else {
// root.file.insert(str);
// }
// return;
//}
void insert(Node& root, string str) {
if (str.empty())
return;
Node* n = (Node*)malloc(sizeof(Node));
int index = str.find('\\');
cout << " find_index:" << index << endl;
if (index != -1) {
string s = str.substr(0, index);
n->data = s;
if (!root.link.insert(*n).second)
for (auto& it : root.link)
if (it.data == n->data) {
*n = it;
break;
}
str = str.substr(index + 1);
insert(*n, str);
}
else {
root.file.insert(str);
}
return;
}
void print(Node root, int n) {
for (int i = 0; i < n; ++i)
cout << " ";
cout << root.data << endl;
for (auto& it : root.link)
print(it, n + 1);
for (auto& it : root.file) {
for (int i = 0; i < n + 1; ++i)
cout << " ";
cout << it << endl;
}
}
int main() {
int N;
string str;
Node root, n;
root.data = "root";
cin >> N;
while (N--) {
cin >> str;
insert(root, str);
}
print(root, 0);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/152908.html
標籤:C++ 語言
