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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <iostream>
using namespace std;
typedef struct tagData {
char path[81];
int size = 0;
tagData* next[501];
}Node;
Node* root;
void strCpy(char* dest, char* str) {
int idx = 0;
while (str[idx]) {
dest[idx] = str[idx];
idx++;
}
dest[idx] = str[idx];
}
int strCmp(char* str1, char* str2) {
int idx = 0;
while (str1[idx] && str2[idx]) {
if (str1[idx] != str2[idx])
break;
idx++;
}
return str1[idx] - str2[idx];
}
int strLen(char* str) {
int idx = 0;
while (str[idx])
idx++;
return idx;
}
Node* createNode() {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->size = 0;
for (int i = 0; i < 501; i++) {
newNode->next[i] = NULL;
}
return newNode;
}
Node *mergeNode[501];
void mergeSort(Node * cur, int left, int mid, int right) {
if (cur == NULL)
return;
int K = left;
int L = left;
int R = mid + 1;
while (L <= mid && R <= right) {
if (strCmp(cur->next[L]->path, cur->next[R]->path) < 0) {
mergeNode[K++] = cur->next[L++];
}
else {
mergeNode[K++] = cur->next[R++];
}
}
for (int i = L; i <= mid; i++) {
mergeNode[K++] = cur->next[L++];
}
for (int i = R; i <= right; i++) {
mergeNode[K++] = cur->next[L++];
}
for (int i = left; i <= right; i++) {
cur->next[i] = mergeNode[i];
}
}
void merge(Node * cur, int left, int right) {
if (left < right) {
int mid = (left + right) / 2;
merge(cur, left, mid);
merge(cur, mid + 1, right);
mergeSort(cur, left, mid, right);
}
}
void printAll(Node * cur, int depth) {
if (cur == NULL)
return;
merge(cur, 0, cur->size - 1);
for (int i = 0; i < cur->size; i++) {
for (int j = 0; j < depth; j++) {
cout << " ";
}
cout << cur->next[i]->path << endl;
printAll(cur->next[i], depth + 1);
}
}
int main() {
int N;
cin >> N;
root = createNode();
root->size = 0;
for (int i = 0; i < N; i++) {
char input[81];
cin >> input;
int strSize = strLen(input);
Node* cur = root;
int idx = 0;
char inStr[81];
int inIdx = 0;
while (input[idx]) {
if (input[idx] == '\' || idx == strSize - 1) {
if (idx == strSize - 1) {
inStr[inIdx++] = input[idx];
}
inStr[inIdx] = '';
//중복체크
int isDuplication = -1;
for (int i = 0; i < cur->size; i++) {
if (cur->next[i] == NULL)
break;
if (strCmp(inStr, cur->next[i]->path) == 0) {
isDuplication = i;
break;
}
}
//노드정리
if (isDuplication == -1) {
cur->next[cur->size] = createNode();
strCpy(cur->next[cur->size]->path, inStr);
cur->size++;
cur = cur->next[cur->size - 1];
}
else {
cur = cur->next[isDuplication];
}
inIdx = 0;
idx++;
continue;
}
inStr[inIdx++] = input[idx];
idx++;
}
}
printAll(root, 0);
return 0;
}
cs


틀렸다는데... 이유를 모르겠습니다ㅠ
고수님들 부탁드려요.