Professional Documents
Culture Documents
IIT2020039
1.
Knuth-Morris and Pratt introduce a linear time algorithm for the string matching problem.
A matching time of O (n) is achieved by avoiding comparison with an element of 'S' that
have previously been involved in comparison with some element of the pattern 'p' to be
matched. i.e., backtracking on the string 'S' never occurs
2.
// C++ program for implementation of KMP pattern searching
// algorithm
#include <bits/stdc++.h>
if (j == M) {
printf("Found pattern at index %d ", i - j);
j = lps[j - 1];
}
The first operation is to insert a new node into the trie. Before we start the
implementation, it is important to understand some points:
Every letter of the input key (word) is inserted as an individual in the Trie_node. Note
that children point to the next level of Trie nodes.
If the present node already has a reference to the present letter, set the present node
to that referenced node. Otherwise, create a new node, set the letter to be equal to the
present letter, and even start the present node with this new node.
The second operation is to search for a node in a Trie. The searching operation is similar to
the insertion operation. The search operation is used to search a key in the trie. The
implementation of the searching operation is shown below.
class Search_Trie {
4.
In Trie structure, we have a field to store end of word marker, we call it isLeaf in below
implementation. To count words, we need to simply traverse the Trie and count all nodes
where isLeaf is set.
// Alphabet size (# of
symbols) #define
ALPHABET_SIZE (26)
// Trie node
struct
TrieNode
{
struct TrieNode *children[ALPHABET_SIZE];
return pNode;
}
pCrawl = pCrawl->children[index];
}
return result;
}
// Driver
int main()
{
// Input keys (use only 'a' through 'z'
// and lower case)
char keys[][8] = {"the", "a", "there", "answer",
"any", "by", "bye", "their"};
// Construct Trie
for (int i = 0; i < ARRAY_SIZE(keys); i+
+) insert(root, keys[i]);
cout <<
wordCount(root);
return 0;
}
5.
Trie* child[MAX_CHAR];
// go to next node
node = node->child[ind];
}
preorder(node->child[i], arr);
}
}
}