📌  相关文章
📜  构造一个字符串,该字符串具有来自给定字符串的恰好 K 个子序列

📅  最后修改于: 2021-09-07 02:03:16             🧑  作者: Mango

给定一个字符串str和一个整数K ,任务是找到一个字符串S ,使其恰好具有给定字符串str 的K个子序列。
例子:

方法:
要解决上述问题,我们必须按照以下步骤操作:

  • 这个想法是找到 K 的主要因素并存储主要因素(比如因素)。
  • 创建一个给定字符串大小的空数组count来存储结果字符串s中每个字符的计数。用 1 初始化数组。
  • 现在从列表中弹出元素因子并以循环方式乘以数组的每个位置,直到列表变空。最后,我们得到了数组中 str 的每个字符的计数。
  • 在数组count[] 中迭代并将每个字符ch的字符数附加到结果字符串s

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
#include 
using namespace std;
 
// Function that computes the string s
void printSubsequenceString(string str,
                            long long k)
{
    // Length of the given string str
    int n = str.size();
    int i;
 
    // List that stores all the prime
    // factors of given k
    vector factors;
 
    // Find the prime factors
    for (long long i = 2;
         i <= sqrt(k); i++) {
 
        while (k % i == 0) {
            factors.push_back(i);
            k /= i;
        }
    }
    if (k > 1)
        factors.push_back(k);
 
    // Initialize the count of each
    // character position as 1
    vector count(n, 1);
 
    int index = 0;
 
    // Loop until the list
    // becomes empty
    while (factors.size() > 0) {
 
        // Increase the character
        // count by multiplying it
        // with the prime factor
        count[index++] *= factors.back();
        factors.pop_back();
 
        // If we reach end then again
        // start from beginning
        if (index == n)
            index = 0;
    }
 
    // Store the output
    string s;
 
    for (i = 0; i < n; i++) {
        while (count[i]-- > 0) {
            s += str[i];
        }
    }
 
    // Print the string
    cout << s;
}
 
// Driver code
int main()
{
    // Given String
    string str = "code";
 
    long long k = 20;
 
    // Function Call
    printSubsequenceString(str, k);
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
 
// Function that computes the String s
static void printSubsequenceString(String str,
                                      int k)
{
    // Length of the given String str
    int n = str.length();
    int i;
 
    // List that stores all the prime
    // factors of given k
    Vector factors = new Vector();
 
    // Find the prime factors
    for (i = 2; i <= Math.sqrt(k); i++)
    {
        while (k % i == 0)
        {
            factors.add(i);
            k /= i;
        }
    }
    if (k > 1)
        factors.add(k);
 
    // Initialize the count of each
    // character position as 1
    int []count = new int[n];
    Arrays.fill(count, 1);
    int index = 0;
 
    // Loop until the list
    // becomes empty
    while (factors.size() > 0)
    {
 
        // Increase the character
        // count by multiplying it
        // with the prime factor
        count[index++] *= factors.get(factors.size() - 1);
        factors.remove(factors.get(factors.size() - 1));
 
        // If we reach end then again
        // start from beginning
        if (index == n)
            index = 0;
    }
 
    // Store the output
    String s = "";
 
    for (i = 0; i < n; i++)
    {
        while (count[i]-- > 0)
        {
            s += str.charAt(i);
        }
    }
 
    // Print the String
    System.out.print(s);
}
 
// Driver code
public static void main(String[] args)
{
    // Given String
    String str = "code";
 
    int k = 20;
 
    // Function Call
    printSubsequenceString(str, k);
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python3 program for
# the above approach
import math
 
# Function that computes
# the string s
def printSubsequenceString(st, k):
    # Length of the given
    # string str
    n = len(st)
 
    # List that stores
    # all the prime
    # factors of given k
    factors = []
   
    # Find the prime factors
    sqt = (int(math.sqrt(k)))
    for i in range (2, sqt + 1):
 
        while (k % i == 0):
            factors.append(i)
            k //= i
    
    if (k > 1):
        factors.append(k)
 
    # Initialize the count of each
    # character position as 1
    count = [1] * n
 
    index = 0
 
    # Loop until the list
    # becomes empty
    while (len(factors) > 0):
 
        # Increase the character
        # count by multiplying it
        # with the prime factor
        count[index] *= factors[-1]
        factors.pop()
        index += 1
 
        # If we reach end then again
        # start from beginning
        if (index == n):
            index = 0
             
    # store output
    s = ""
    for i in range (n):
        while (count[i] > 0):
            s += st[i]
            count[i] -= 1 
        
    # Print the string
    print (s)
 
# Driver code
if __name__ == "__main__":
   
    # Given String
    st = "code"
 
    k = 20
 
    # Function Call
    printSubsequenceString(st, k)
     
# This code is contributed by Chitranayal


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function that computes the String s
static void printSubsequenceString(String str,
                                      int k)
{
    // Length of the given String str
    int n = str.Length;
    int i;
 
    // List that stores all the prime
    // factors of given k
    List factors = new List();
 
    // Find the prime factors
    for (i = 2; i <= Math.Sqrt(k); i++)
    {
        while (k % i == 0)
        {
            factors.Add(i);
            k /= i;
        }
    }
    if (k > 1)
        factors.Add(k);
 
    // Initialize the count of each
    // character position as 1
    int []count = new int[n];
    for (i = 0; i < n; i++)
        count[i] = 1;
    int index = 0;
 
    // Loop until the list
    // becomes empty
    while (factors.Count > 0)
    {
 
        // Increase the character
        // count by multiplying it
        // with the prime factor
        count[index++] *= factors[factors.Count - 1];
        factors.Remove(factors[factors.Count - 1]);
 
        // If we reach end then again
        // start from beginning
        if (index == n)
            index = 0;
    }
 
    // Store the output
    String s = "";
 
    for (i = 0; i < n; i++)
    {
        while (count[i]-- > 0)
        {
            s += str[i];
        }
    }
 
    // Print the String
    Console.Write(s);
}
 
// Driver code
public static void Main(String[] args)
{
    // Given String
    String str = "code";
 
    int k = 20;
 
    // Function Call
    printSubsequenceString(str, k);
}
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:

cccccoodde

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live