#include<bits/stdc++.h>
#define MAX 1000
using
namespace
std;
struct
trieNode
{
struct
trieNode *children[26];
int
weight;
};
struct
trieNode* getNode()
{
struct
trieNode *node =
new
trieNode;
node -> weight = INT_MIN;
for
(
int
i = 0; i < 26; i++)
node -> children[i] = NULL;
}
struct
trieNode* insert(
char
str[],
int
wt,
int
idx,
struct
trieNode* root)
{
int
cur = str[idx] -
'a'
;
if
(!root -> children[cur])
root -> children[cur] = getNode();
root->children[cur]->weight =
max(root->children[cur]->weight, wt);
if
(idx + 1 !=
strlen
(str))
root -> children[cur] =
insert(str, wt, idx + 1, root -> children[cur]);
return
root;
}
int
searchMaximum(
char
prefix[],
struct
trieNode *root)
{
int
idx = 0, n =
strlen
(prefix), ans = -1;
while
(idx < n)
{
int
cur = prefix[idx] -
'a'
;
if
(!root->children[cur])
return
-1;
ans = root->children[cur]->weight;
root = root->children[cur];
++idx;
}
return
ans;
}
int
maxWeight(
char
str[MAX][MAX],
int
weight[],
int
n,
char
prefix[])
{
struct
trieNode* root = getNode();
for
(
int
i = 0; i < n; i++)
root = insert(str[i], weight[i], 0, root);
return
searchMaximum(prefix, root);
}
int
main()
{
int
n = 3;
char
str[3][MAX] = {
"geeks"
,
"geeksfor"
,
"geeksforgeeks"
};
int
weight[] = {15, 30, 45};
char
prefix[] =
"geek"
;
cout << maxWeight(str, weight, n, prefix) << endl;
return
0;
}