-
Notifications
You must be signed in to change notification settings - Fork 1
/
trie.c
171 lines (137 loc) · 3.07 KB
/
trie.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
///////////////////////////////////////////////////////////////////////
// Desenvolvedor: Douglas Detoni
// Email: ddetoni@inf.ufpel.edu.br
///////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 6000
typedef struct node{
char c;
struct node **vector;
int final;
} node;
node * makeNode(char w){
node * root = malloc(sizeof(node));
root->c = w;
root->vector = malloc(sizeof(node *) * 4);
root->vector[0] = NULL;
root->vector[1] = NULL;
root->vector[2] = NULL;
root->vector[3] = NULL;
root->final = 0;
return root;
}
int insertWord(node *root, char word[], int onset){
int i, k=onset;
node *auxNode = root;
int sizeW = strlen(word);
while(k != sizeW){
//printf("%c", word[k]);
for(i=0; i<4; i++){
if(auxNode->vector[i] == NULL){
auxNode->vector[i] = makeNode(word[k]);
auxNode = auxNode->vector[i];
break;
}else
{
if(auxNode->vector[i]->c == word[k]){
auxNode = auxNode->vector[i];
break;
}
}
}
k++;
}
auxNode->final = 1;
//printf("\n");
return 1;
}
int searchWord(node *root, char word[]){
node *auxNode = root;
int size = strlen(word);
int i, k=0;
while(k != size){
for(i=0; i<4; i++){
if(auxNode->vector[i] != NULL){
if(auxNode->vector[i]->c == word[k]){
auxNode = auxNode->vector[i];
break;
}
} else
{
return 0;
}
}
k++;
}
if(auxNode->final == 1)
return 1;
else
return 0;
}
char * searchSubstring(node *root, char *word, int onset){
node *auxNode = root;
int size = strlen(word);
int i, k=onset, counter=0;
char *substring = malloc(sizeof(char)*SIZE);
while(k != size){
for(i=0; i<4; i++){
if(auxNode->vector[i] != NULL){
if(auxNode->vector[i]->c == word[k]){
auxNode = auxNode->vector[i];
substring[counter] = word[k];
counter++;
break;
}
} else
{
substring[counter] = '\n';
return substring;
}
}
k++;
}
substring[counter] = '\n';
return substring;
}
int searchBiggerSubstring(node *root, char *word){
int i, size, sizeSub=0, contWords;
char *substring=malloc(sizeof(char)*(SIZE/2)), *auxSubstring = malloc(sizeof(char)*(SIZE/2));
node *trie;
trie = makeNode(' ');
size = strlen(word);
for(i=0; i< size; i++){
auxSubstring = searchSubstring(root, word, i);
if(strlen(auxSubstring) > sizeSub){
strcpy(substring, auxSubstring);
sizeSub = strlen(substring);
contWords=1;
insertWord(trie, auxSubstring, 0);
} else if(strlen(auxSubstring) == sizeSub){
if(!searchWord(trie, auxSubstring)){
insertWord(trie, auxSubstring, 0);
strcat(substring, auxSubstring);
contWords++;
}
}
}
for(i=0; i<strlen(substring); i++){
if((i/contWords)==sizeSub)
printf("\n");
printf("%c", substring[i]);
}
return 1;
}
int main(){
node *root;
char word[SIZE], otherWord[SIZE];
int i;
scanf("%s", word);
scanf("%s", otherWord);
root = makeNode(' ');
for(i=0; i < strlen(word); i++)
insertWord(root, word, i);
searchBiggerSubstring(root, otherWord);
return 0;
}