📜  由字符串 S 作为子字符串恰好 K 次组成的最小字符串

📅  最后修改于: 2021-09-17 07:32:59             🧑  作者: Mango

给定长度为N和整数K的字符串S ,找到包含字符串S作为子字符串恰好K次的最小长度字符串。

例子:

方法:为了优化上述方法,找到最长的适当前缀,它也是给定字符串S的后缀,然后生成S 的一个子串,不包括最长公共前缀,并将这个子串添加到答案中正好K – 1次到原始字符串。请按照以下步骤解决问题:

  • 使用 KMP 算法找到最长适当前缀的长度。
  • 将子字符串S.substring(N-lps[N-1])附加到 S,正好K-1次。
  • 打印答案。

    下面是上述方法的实现。

    C++
    // C++ Program to implement 
    // the above approach 
    #include  
    using namespace std; 
      
    // KMP algorithm 
    int* kmp(string& s) 
    { 
      
        int n = s.size(); 
        int* lps = new int[n]; 
      
        lps[0] = 0; 
        int i = 1, len = 0; 
        while (i < n) { 
      
            if (s[i] == s[len]) { 
                len++; 
                lps[i] = len; 
                i++; 
            } 
            else { 
                if (len != 0) { 
                    len = lps[len - 1]; 
                } 
                else { 
                    lps[i] = 0; 
                    i++; 
                } 
            } 
        } 
      
        return lps; 
    } 
      
    // Function to return the required string 
    string findString(string& s, int k) 
    { 
      
        int n = s.length(); 
      
        // Finding the longest proper prefix 
        // which is also suffix 
        int* lps = kmp(s); 
      
        // ans string 
        string ans = ""; 
      
        string suff 
            = s.substr(0, n - lps[n - 1]); 
      
        for (int i = 0; i < k - 1; ++i) { 
      
            // Update ans appending the 
            // substring K - 1 times 
            ans += suff; 
        } 
      
        // Append the original string 
        ans += s; 
      
        // Returning min length string 
        // which contain exactly k 
        // substring of given string 
        return ans; 
    } 
      
    // Driver Code 
    int main() 
    { 
      
        int k = 3; 
      
        string s = "geeksforgeeks"; 
      
        cout << findString(s, k) << endl; 
    }


    Java
    // Java program to implement 
    // the above approach 
    import java.util.*;
      
    class GFG{
          
    // KMP algorithm 
    static int[] kmp(String s) 
    { 
        int n = s.length(); 
        int[] lps = new int[n]; 
        lps[0] = 0; 
      
        int i = 1, len = 0; 
          
        while (i < n)
        {
            if (s.charAt(i) == s.charAt(len))
            { 
                len++; 
                lps[i] = len; 
                i++; 
            } 
            else 
            { 
                if (len != 0)
                { 
                    len = lps[len - 1]; 
                } 
                else
                { 
                    lps[i] = 0; 
                    i++; 
                } 
            } 
        } 
        return lps; 
    } 
      
    // Function to return the required string 
    static String findString(String s, int k) 
    { 
        int n = s.length(); 
      
        // Finding the longest proper prefix 
        // which is also suffix 
        int[] lps = kmp(s); 
      
        // ans string 
        String ans = ""; 
      
        String suff    = s.substring(0, n - lps[n - 1]); 
      
        for(int i = 0; i < k - 1; ++i) 
        { 
              
            // Update ans appending the 
            // substring K - 1 times 
            ans += suff; 
        } 
      
        // Append the original string 
        ans += s; 
      
        // Returning min length string 
        // which contain exactly k 
        // substring of given string 
        return ans; 
    }
      
    // Driver code
    public static void main (String[] args) 
    {
        int k = 3; 
          
        String s = "geeksforgeeks"; 
          
        System.out.println(findString(s, k)); 
    }
    }
      
    // This code is contributed by offbeat


    Python3
    # Python3 program to implement 
    # the above approach 
      
    # KMP algorithm 
    def kmp(s): 
          
        n = len(s) 
        lps = [None] * n 
        lps[0] = 0
      
        i, Len = 1, 0
          
        while (i < n):
          
            if (s[i] == s[Len]):
                Len += 1
                lps[i] = Len
                i += 1
                  
            else:
                if (Len != 0): 
                    Len = lps[Len - 1]
                else:
                    lps[i] = 0
                    i += 1
                      
        return lps
          
    # Function to return the required string 
    def findString(s, k): 
      
        n = len(s) 
      
        # Finding the longest proper prefix 
        # which is also suffix 
        lps = kmp(s) 
      
        # ans string 
        ans = "" 
          
        suff = s[0: n - lps[n - 1] : 1]
      
        for i in range(k - 1): 
              
            # Update ans appending the 
            # substring K - 1 times 
            ans += suff
      
        # Append the original string 
        ans += s
      
        # Returning min length string 
        # which contain exactly k 
        # substring of given string 
        return ans
      
    # Driver code
    k = 3
          
    s = "geeksforgeeks"
          
    print(findString(s, k)) 
      
    # This code is contributed by divyeshrabadiya07


    C#
    // C# program to implement 
    // the above approach 
    using System;
      
    class GFG{
          
    // KMP algorithm 
    static int[] kmp(string s) 
    { 
        int n = s.Length; 
        int[] lps = new int[n]; 
        lps[0] = 0; 
      
        int i = 1, len = 0; 
          
        while (i < n)
        {
            if (s[i] == s[len])
            { 
                len++; 
                lps[i] = len; 
                i++; 
            } 
            else
            { 
                if (len != 0)
                { 
                    len = lps[len - 1]; 
                } 
                else
                { 
                    lps[i] = 0; 
                    i++; 
                } 
            } 
        } 
        return lps; 
    } 
      
    // Function to return the required string 
    static string findString(string s, int k) 
    { 
        int n = s.Length; 
      
        // Finding the longest proper prefix 
        // which is also suffix 
        int[] lps = kmp(s); 
      
        // ans string 
        string ans = ""; 
      
        string suff = s.Substring(0, 
                                  n - lps[n - 1]); 
      
        for(int i = 0; i < k - 1; ++i) 
        { 
              
            // Update ans appending the 
            // substring K - 1 times 
            ans += suff; 
        } 
      
        // Append the original string 
        ans += s; 
      
        // Returning min length string 
        // which contain exactly k 
        // substring of given string 
        return ans; 
    }
      
    // Driver code
    public static void Main (string[] args) 
    {
        int k = 3; 
          
        string s = "geeksforgeeks"; 
          
        Console.Write(findString(s, k)); 
    }
    }
      
    // This code is contributed by rutvik_56


    输出:
    geeksforgeeksforgeeksforgeeks
    

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

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