Skip to content

Commit 1a80b55

Browse files
committed
2020. 05. 15. Tried #4195
1 parent def0d53 commit 1a80b55

File tree

2 files changed

+60
-17
lines changed

2 files changed

+60
-17
lines changed

05_tree/5_baekjoon/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,7 @@
8080

8181
## [4195번](https://www.acmicpc.net/problem/4195) 친구 네트워크
8282
> 1-4회 : 시간초과 <br>
83-
> 5회 : [시도중](./baekjoon_04195_virtual_friends.c)
83+
> 5회 : 실패 <br>
84+
> 6회 : [시도중](./baekjoon_04195_virtual_friends.c)
8485
- 유니온파인드로 구현
86+
- 시간초과 해결하기 위해 이름(string)탐색에 트라이 적용

05_tree/5_baekjoon/baekjoon_04195_virtual_friends.c

+57-16
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: mihykim <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2020/04/25 15:47:45 by mihykim #+# #+# */
9-
/* Updated: 2020/04/25 19:26:15 by mihykim ### ########.fr */
9+
/* Updated: 2020/05/15 17:00:01 by mihykim ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -27,35 +27,74 @@
2727
#include <stdlib.h>
2828
#include <math.h>
2929

30+
# define ALPHABET 52
31+
# define NONE -1
32+
33+
typedef struct s_node
34+
{
35+
struct s_node *next[ALPHABET];
36+
int index;
37+
} t_node;
38+
39+
t_node *root[ALPHABET];
3040
char name[200001][21];
3141
int parent[200000];
3242
int size[200000];
3343
int n;
3444

35-
int name_to_idx(char input[])
45+
t_node *activate_node(void)
3646
{
47+
t_node *node;
3748
int i = 0;
3849

39-
while (i < n)
50+
node = malloc(sizeof(t_node));
51+
while (i < ALPHABET)
52+
node->next[i++] = NULL;
53+
node->index = NONE;
54+
return (node);
55+
}
56+
57+
int ft_atoi(char c)
58+
{
59+
if (c >= 'a' && c <= 'z')
60+
return (c - 'a');
61+
return (c - 'A');
62+
}
63+
64+
int ft_trie(char *name)
65+
{
66+
int i, j;
67+
t_node *curr;
68+
69+
j = ft_atoi(name[0]);
70+
if (root[j] == NULL)
71+
root[j] = activate_node();
72+
curr = root[j];
73+
for (i = 1; name[i]; i++)
74+
{
75+
j = ft_atoi(name[i]);
76+
if (curr->next[j] == NULL)
77+
curr->next[j] = activate_node();
78+
curr = curr->next[j];
79+
}
80+
if (curr->index == NONE)
4081
{
41-
if (strcmp(input, name[i]) == 0)
42-
return (i);
43-
i++;
82+
curr->index = n;
83+
parent[n] = n;
84+
size[n] = 1;
85+
n++;
4486
}
45-
strcpy(name[n], input);
46-
parent[n] = n;
47-
size[n] = 1;
48-
return (n++);
87+
return (curr->index);
4988
}
5089

51-
int find(int i)
90+
int ft_find(int i)
5291
{
5392
if (i == parent[i])
5493
return (i);
55-
return (parent[i] = find(parent[i]));
94+
return (parent[i] = ft_find(parent[i]));
5695
}
5796

58-
int union_by_size(int a, int b)
97+
int ft_union(int a, int b)
5998
{
6099
int tmp;
61100

@@ -81,14 +120,16 @@ int main(void)
81120
scanf("%d", &t);
82121
while (t--)
83122
{
123+
for (int i = 0; i < ALPHABET; i++)
124+
root[i] = activate_node();
84125
scanf("%d", &f);
85126
n = 0;
86127
while(f--)
87128
{
88129
scanf("%s %s", name1, name2);
89-
a = name_to_idx(name1);
90-
b = name_to_idx(name2);
91-
printf("%d\n", union_by_size(find(a), find(b)));
130+
a = ft_trie(name1);
131+
b = ft_trie(name2);
132+
printf("%d\n", ft_union(ft_find(a), ft_find(b)));
92133
}
93134
}
94135
return (0);

0 commit comments

Comments
 (0)