-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
80 lines (70 loc) · 2.01 KB
/
main.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
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#define ALPHABET_SIZE 26
typedef struct TrieNode {
struct TrieNode *children[ALPHABET_SIZE];
bool isEndOfWord;
} TrieNode;
TrieNode* createNode() {
TrieNode* pNode = (TrieNode*)malloc(sizeof(TrieNode));
if (pNode) {
pNode->isEndOfWord = false;
for (int i = 0; i < ALPHABET_SIZE; i++) {
pNode->children[i] = NULL;
}
}
return pNode;
}
void insert(TrieNode *root, const char *key) {
TrieNode *pCrawl = root;
for (int i = 0; i < strlen(key); i++) {
int index = key[i] - 'a';
if (!pCrawl->children[index]) {
pCrawl->children[index] = createNode();
}
pCrawl = pCrawl->children[index];
}
pCrawl->isEndOfWord = true;
}
void searchTrie(TrieNode *root, const char *prefix, char *currentWord) {
if (!root) return;
if (root->isEndOfWord) {
printf("%s\n", currentWord);
}
for (int i = 0; i < ALPHABET_SIZE; i++) {
if (root->children[i]) {
char c = 'a' + i;
strncat(currentWord, &c, 1);
searchTrie(root->children[i], prefix, currentWord);
currentWord[strlen(currentWord) - 1] = '\0'; // Remove the last character
}
}
}
void autocomplete(TrieNode *root, const char *prefix) {
TrieNode *pCrawl = root;
for (int i = 0; i < strlen(prefix); i++) {
int index = prefix[i] - 'a';
if (!pCrawl->children[index]) {
return; // No words with given prefix
}
pCrawl = pCrawl->children[index];
}
char currentWord[1024] = {0};
strncpy(currentWord, prefix, strlen(prefix));
searchTrie(pCrawl, prefix, currentWord);
}
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <prefix>\n", argv[0]);
return 1;
}
TrieNode *root = createNode();
char word[1024];
while (scanf("%s", word) != EOF) {
insert(root, word);
}
autocomplete(root, argv[1]);
return 0;
}