-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy path2.10.cpp
90 lines (82 loc) · 2.15 KB
/
2.10.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* 题目名称:路经打印
* 题目来源:上海交通大学复试上机题
* 题目链接:http://t.cn/E9dvHs4
* 代码作者:杨泽邦(炉灰)
*/
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
const string space = " ";
struct Node {
string data;
Node* child;
Node* brother;
Node() {}
Node(string s, Node* c = nullptr, Node* b = nullptr): data(s), child(c), brother(b) {}
};
Node* Insert(Node* root, vector<string> strings, int index) {
if (index >= strings.size()) {
return nullptr;
}
Node* p = root->child;
if (p == nullptr || strings[index] < p->data) {
root->child = new Node(strings[index], nullptr, p);
Insert(root->child, strings, index + 1);
} else {
while (p->brother != nullptr && p->brother->data <= strings[index]) {
p = p->brother;
}
if (p->data == strings[index]) {
Insert(p, strings, index + 1);
} else {
Node* q = new Node(strings[index], nullptr, p->brother);
p->brother = q;
Insert(q, strings, index + 1);
}
}
return root;
}
void Visit(Node* root, int number) {
if (root == nullptr) {
return;
}
for (int i = 0; i < number; ++i) {
cout << space;
}
cout << root->data << endl;
Visit(root->child, number + 1);
Visit(root->brother, number);
}
vector<string> Get(string str) {
vector<string> strings;
int start = 0;
for (int i = 0; i < str.size(); ++i) {
if (str[i] == '\\') {
strings.push_back(str.substr(start, i - start));
start = i + 1;
} else if (i == str.size() - 1) {
strings.push_back(str.substr(start, i - start + 1));
}
}
return strings;
}
int main() {
int n;
while (scanf("%d", &n) != EOF) {
if (n == 0) {
break;
}
Node* root = new Node("");
while (n--) {
string str;
cin >> str;
vector<string> strings = Get(str);
Insert(root, strings, 0);
}
Visit(root->child, 0);
cout << endl;
}
return 0;
}