📌  相关文章
📜  字典序最小的字符串从一个字符串的前K字符补上的字符形成| 2套

📅  最后修改于: 2021-10-28 01:58:33             🧑  作者: Mango

给定一个由小写字母和整数K组成的字符串str ,您可以对str执行以下操作

  1. 初始化一个空字符串X = “”
  2. str的前K 个字符中取出任何字符并将其附加到X
  3. str 中删除所选字符。
  4. 当 str 中剩余字符时,重复上述步骤。

任务是生成X使其尽可能按字典顺序最小,然后打印生成的字符串。

例子:

方法:为了得到字典序最小的字符串 ,每次从str 中选择一个字符时,都需要从前K 个字符取最小的字符。为此,我们可以将前K 个字符放在 priority_queue(最小堆)中,然后选择最小的字符并将其附加到X 。然后,将str 中的下一个字符推送到优先级队列并重复该过程,直到有剩余字符需要处理。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the lexicographically 
// smallest required string
string getSmallestStr(string S, int K)
{
  
    // Initially empty string
    string X = "";
  
    // min heap of characters
    priority_queue, greater > pq;
  
    // Length of the string
    int i, n = S.length();
  
    // K cannot be greater than
    // the size of the string
    K = min(K, n);
  
    // First push the first K characters
    // into the priority_queue
    for (i = 0; i < K; i++)
        pq.push(S[i]);
  
    // While there are characters to append
    while (!pq.empty()) {
  
        // Append the top of priority_queue to X
        X += pq.top();
  
        // Remove the top element
        pq.pop();
  
        // Push only if i is less than
        // the size of string
        if (i < S.length())
            pq.push(S[i]);
  
        i++;
    }
  
    // Return the generated string
    return X;
}
  
// Driver code
int main()
{
    string S = "geeksforgeeks";
    int K = 5;
  
    cout << getSmallestStr(S, K);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.PriorityQueue;
  
class GFG 
{
  
    // Function to return the lexicographically
    // smallest required string
    static String getSmallestStr(String S, int K)
    {
  
        // Initially empty string
        String X = "";
  
        // min heap of characters
        PriorityQueue pq = new PriorityQueue<>();
  
        // Length of the string
        int i, n = S.length();
  
        // K cannot be greater than
        // the size of the string
        K = Math.min(K, n);
  
        // First push the first K characters
        // into the priority_queue
        for (i = 0; i < K; i++)
            pq.add(S.charAt(i));
  
        // While there are characters to append
        while (!pq.isEmpty()) 
        {
  
            // Append the top of priority_queue to X
            X += pq.peek();
  
            // Remove the top element
            pq.remove();
  
            // Push only if i is less than
            // the size of string
            if (i < S.length())
                pq.add(S.charAt(i));
                  
            i++;
        }
  
        // Return the generated string
        return X;
    }
  
    // Driver Code
    public static void main(String[] args) 
    {
        String S = "geeksforgeeks";
        int K = 5;
        System.out.println(getSmallestStr(S, K));
    }
}
  
// This code is contributed by
// sanjeev2552


输出:
eefggeekkorss

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