-
Notifications
You must be signed in to change notification settings - Fork 0
/
LongestPrefixMatch.cpp
125 lines (103 loc) · 2.2 KB
/
LongestPrefixMatch.cpp
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
#include<iostream>
#include<string>
#include<vector>
using namespace std;
#define CHAR_TO_INDEX(c) c - 'a'
#define AS 26
struct TrieNode
{
int val;
struct TrieNode* child[26];
};
class Trie
{
int K;
struct TrieNode* root; // root is the dummy node
struct TrieNode* createNode(int val);
public:
Trie();
void insert(string s);
string getLongestMatch(string s);
};
Trie::Trie()
{
this->K = 0;
this->root = createNode(-1);
}
void Trie::insert(string s)
{
this->K++;
struct TrieNode* temp = root;
int len = s.length();
for(int i = 0; i < len; i++)
{
int ci = CHAR_TO_INDEX(s[i]);
if(temp->child[ci] == NULL)
{
temp->child[ci] = createNode(0);
}
temp = temp->child[ci];
}
temp->val = K;
}
string Trie::getLongestMatch(string s)
{
int len = s.length();
struct TrieNode* temp = root;
pair<int,string> ansp;
int count = 0;
for(int i = 0; i < len; i++)
{
int ci = CHAR_TO_INDEX(s[i]);
temp = temp->child[ci];
count++;
if(temp == NULL)
break;
if(temp->val != 0 )
{
ansp.first = count;
ansp.second = s.substr(0,i + 1);
}
}
return ansp.second;
}
struct TrieNode* Trie::createNode(int val)
{
struct TrieNode* temp = new struct TrieNode;
temp->val = val;
for(int i = 0; i < AS; i++)
{
temp->child[i] = NULL;
}
return temp;
}
class SolnLongestPrefixMatch
{
Trie dict;
public:
SolnLongestPrefixMatch(vector<string> &ls);
string ans(string s);
};
SolnLongestPrefixMatch::SolnLongestPrefixMatch(vector<string> &ls)
{
for(int i = 0; i < ls.size(); i++)
{
dict.insert(ls[i]);
}
}
string SolnLongestPrefixMatch::ans(string s)
{
return dict.getLongestMatch(s);
}
int main()
{
vector<string> v({"are","area","base","cat","cater","basement"});
SolnLongestPrefixMatch slpm(v);
cout << slpm.ans("caterer") << endl;
cout << slpm.ans("basement") << endl;
cout << slpm.ans("are") << endl;
cout << slpm.ans("arex") << endl;
cout << slpm.ans("basemexz") << endl;
cout << slpm.ans("xyz");
return 0;
}