📜  当最小值字符串在每个操作中连接时打印最终字符串

📅  最后修改于: 2022-05-13 01:57:00.925000             🧑  作者: Mango

当最小值字符串在每个操作中连接时打印最终字符串

给定一个字符串数组和一个整数数组,其中数组的第 i整数对应于字符串数组中存在的第i字符串的值。现在选择整数数组中具有最小值的两个字符串,并将两个整数相加并连接字符串,并将求和的整数添加到整数数组中,并将连接后的字符串添加到字符串数组中,并继续重复整个过程,直到两个数组中只剩下一个元素。另外,请注意,一旦选择了任何两个字符串和整数,它们就会从数组中删除,并且新的字符串和整数会被添加回各自的数组中。
任务是打印获得的最终字符串。

例子:

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程。

方法:思路是使用优先级队列,制作一对字符串及其对应的值存储在优先级队列中,不断从优先级队列中取出两对,将整数相加并连接字符串,然后再入队进入优先级队列,直到队列的大小减为1,然后打印队列中唯一剩余的字符串。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Class that represents a pair
struct Priority
{
    string s;
    int count;
};
 
struct Compare
{
    int operator()(Priority a, Priority b)
    {
        if (a.count > b.count)
            return 1;
        else if (a.count < b.count)
            return -1;
             
        return 0;
    }
};
 
// Function that prints the final string
static void FindString(string str[], int num[],
                                     int n)
{
    priority_queue, Compare> p;
     
    // Add all the strings and their corresponding
    // values to the priority queue
    for(int i = 0; i < n; i++)
    {
        Priority x;
        x.s = str[i];
        x.count = num[i];
        p.push(x);
    }
     
    // Take those two strings from the priority
    // queue whose corresponding integer values
    // are smaller and add them up as well as
    // their values and add them back to the
    // priority queue while there are more
    // than a single element in the queue
    while (p.size() > 1)
    {
         
        // Get the minimum valued string
        Priority x = p.top();
        p.pop();
        // p.remove(x);
         
        // Get the second minimum valued string
        Priority y = p.top();
        p.pop();
         
        // Updated integer value
        int temp = x.count + y.count;
        string sb = x.s + y.s;
         
        // Create new entry for the queue
        Priority z;
        z.count = temp;
        z.s = sb;
         
        // Add to the queue
        p.push(z);
    }
     
    // Print the only remaining string
    Priority z = p.top();
    p.pop();
     
    cout << z.s << endl;
}
 
// Driver code
int main()
{
    string str[] = { "Geeks", "For", "Geeks" };
    int num[] = { 2, 3, 7 };
    int n = sizeof(num) / sizeof(int);
     
    FindString(str, num, n);
}
 
// This code is contributed by sanjeev2552


Java
// Java implementation of the approach
import java.util.*;
import java.lang.*;
 
// Class that represents a pair
class Priority {
    String s;
    int count;
}
 
class PQ implements Comparator {
    public int compare(Priority a, Priority b)
    {
        if (a.count > b.count)
            return 1;
        else if (a.count < b.count)
            return -1;
        return 0;
    }
}
 
class GFG {
 
    // Function that prints the final string
    static void FindString(String str[], int num[], int n)
    {
        Comparator comparator = new PQ();
        PriorityQueue p
            = new PriorityQueue(comparator);
 
        // Add all the strings and their corresponding
        // values to the priority queue
        for (int i = 0; i < n; i++) {
            Priority x = new Priority();
            x.s = str[i];
            x.count = num[i];
            p.add(x);
        }
 
        // Take those two strings from the priority
        // queue whose corresponding integer values are smaller
        // and add them up as well as their values and
        // add them back to the priority queue
        // while there are more than a single element in the queue
        while (p.size() > 1) {
 
            // Get the minimum valued string
            Priority x = p.poll();
            p.remove(x);
 
            // Get the second minimum valued string
            Priority y = p.poll();
            p.remove(y);
 
            // Updated integer value
            int temp = x.count + y.count;
            String sb = x.s + y.s;
 
            // Create new entry for the queue
            Priority z = new Priority();
            z.count = temp;
            z.s = sb;
 
            // Add to the queue
            p.add(z);
        }
 
        // Print the only remaining string
        Priority z = p.poll();
        System.out.println(z.s);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        String str[] = { "Geeks", "For", "Geeks" };
        int num[] = { 2, 3, 7 };
        int n = num.length;
 
        FindString(str, num, n);
    }
}


Javascript


输出:
GeeksForGeeks

时间复杂度: O(N * log(N))
辅助空间: O(N)