-
Notifications
You must be signed in to change notification settings - Fork 1
/
baekjoon_14425_string_set.c
101 lines (92 loc) · 2.37 KB
/
baekjoon_14425_string_set.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* baekjoon_14425_string_set.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mihykim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/04/21 15:52:27 by mihykim #+# #+# */
/* Updated: 2020/04/25 22:05:54 by mihykim ### ########.fr */
/* */
/* ************************************************************************** */
/*
** - Write a program that tells how many strings are included in the set S.
** - Input will be like :
** 2 <= N : # of strings in set S
** 3 <= M : # of strings to scan
** baekjoon
** codeplus
** codeplus
** codeminus
** sundaycoding
*/
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
typedef struct s_node
{
struct s_node *next[26];
bool finish;
} t_node;
t_node *root[26];
char str[501];
t_node *activate_node(void)
{
t_node *node;
int i = 0;
node = malloc(sizeof(t_node));
while (i < 26)
node->next[i++] = NULL;
node->finish = false;
return (node);
}
int main(void)
{
t_node *curr;
int N, M;
int i = 0;
int j;
int count = 0;
while (i < 26)
root[i++] = activate_node();
scanf("%d %d\n", &N, &M);
while(N--)
{
scanf("%s", str);
j = str[0] - 'a';
if (root[j] == NULL)
root[j] = activate_node();
curr = root[j];
for (i = 1 ; str[i] ; i++)
{
j = str[i] - 'a';
if (curr->next[j] == NULL)
curr->next[j] = activate_node();
curr = curr->next[j];
}
curr->finish = true;
}
while (M--)
{
scanf("%s", str);
j = str[0] - 'a';
if (root[j] == NULL)
continue ;
curr = root[j];
for (i = 1 ; str[i] ; i++)
{
j = str[i] - 'a';
if (curr->next[j] == NULL)
break ;
curr = curr->next[j];
}
if (str[i] == '\0' && curr->finish == true)
count++;
}
printf("%d", count);
return (0);
}
/*
** line 89 : Changed from 'continue' to 'break'
** line 91 : Added str[i] == '\0' condition
*/