原文:

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

示例:

输入: str = {“极客”、“for”、“极客”},num = {2,3,7} 输出:极客 forgeeks 选择 2 和 3 添加它们,形成“极客 for”和 5 将它们添加回数组,str = {“极客 for”、“极客”},num = {5,7} 现在选择 7 和 5 添加它们,形成“极客 forgeeks”,这是最后的字符串。

输入:str = { " abc "," def "," ghi "," jkl " },num = {1,4,2,6} 输出:jklabcghdef

方法:思路是使用一个,制作一对字符串及其对应的值,存储在优先级队列中,保持两对从优先级队列中出列,将整数相加并串接,重新排入优先级队列,直到队列的大小减为 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);
    }
}

java 描述语言


output: 

geeksforgeeks

时间复杂度: o(n * log(n)) 辅助空间: o(n)