Weighted Prefix Search

Given n strings and a weight associated with each string. The task is to find the maximum weight of string having the given prefix. Print “-1” if no string is present with given prefix.

Examples:

Input : 
s1 = "geeks", w1 = 15
s2 = "geeksfor", w2 = 30
s3 = "geeksforgeeks", w3 = 45
prefix = geek
Output : 45

All the string contain the given prefix, but
maximum weight of string is 45 among all.

Method 1: (Brute Force)

Check all the string for given prefix, if string contains the prefix, compare its weight with maximum value so far.

Below is C++ implementation of this approach:

// C++ program to find the maximum weight with given prefix.
// Brute Force based C++ program to find the
// string with maximum weight and given prefix.
#include<bits/stdc++.h>
#define MAX 1000
using namespace std;

// Return the maximum weight of string having
// given prefix.
int maxWeight(char str[MAX][MAX], int weight[],
                          int n, char prefix[])
{
    int ans = -1;
    bool check;

    // Traversing all strings
    for (int i = 0; i < n; i++)
    {
        check = true;

        // Checking if string contain given prefix.
        for (int j=0, k=0; j < strlen(str[i]) &&
                           k < strlen(prefix); j++, k++)
        {
            if (str[i][j] != prefix[k])
            {
                check = false;
                break;
            }
        }

        // If contain prefix then finding
        // the maximum value.
        if (check)
            ans = max(ans, weight[i]);
    }

    return ans;
}

// Driven program
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;
}

Output:

45

 

Method 2 (efficient):

The idea is to create and maintain a Trie. Instead of the normal Trie where we store the character, store a number with it, which is maximum value of its prefix. When we encounter the prefix again update the value with maximum of existing and new one.
Now, search prefix for maximum value, run through the characters starting from the root, if one of character is missing return -1, else return the number stored in the root.

Below is the C++ implementation of this approach:

// C++ program to find the maximum weight
// with given prefix.
#include<bits/stdc++.h>
#define MAX 1000
using namespace std;

// Structure of a trie node
struct trieNode
{
    // Pointer its children.
    struct trieNode *children[26];

    // To store weight of string.
    int weight;
};

// Create and return a Trie node
struct trieNode* getNode()
{
    struct trieNode *node = new trieNode;
    node -> weight = INT_MIN;

    for (int i = 0; i < 26; i++)
        node -> children[i] = NULL;
}

// Inserting the node in the Trie.
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();

    // Assigning the maximum weight
    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;
}

// Search and return the maximum weight.
int searchMaximum(char prefix[], struct trieNode *root)
{
    int idx = 0, n = strlen(prefix), ans = -1;

    // Searching the prefix in TRie.
    while (idx < n)
    {
        int cur = prefix[idx] - 'a';

        // If prefix not found return -1.
        if (!root->children[cur])
            return -1;

        ans = root->children[cur]->weight;
        root = root->children[cur];
        ++idx;
    }

    return ans;
}

// Return the maximum weight of string having given prefix.
int maxWeight(char str[MAX][MAX], int weight[], int n,
                                       char prefix[])
{
    struct trieNode* root = getNode();

    // Inserting all string in the Trie.
    for (int i = 0; i < n; i++)
        root = insert(str[i], weight[i], 0, root);

    return searchMaximum(prefix, root);

}

// Driven Program
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;
}
45

This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:







Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.