📌  相关文章
📜  给定字符串的第 K 个字典序最小的唯一子字符串

📅  最后修改于: 2021-10-25 06:22:39             🧑  作者: Mango

给定一个字符串S。任务是按字典顺序打印第 K 个 s 的不同子字符串中最小的一个。
s 的子串是将 s 中的一个非空连续部分取出得到的字符串。
例如,如果 s = ababc,则 a、bab 和 ababc 是 s 的子串,而 ac、z 和空字符串则不是。另外,我们说,当他们作为字符串不同子是不同的。

例子:

方法:对于任意字符串t,其每个适当的后缀在字典上都小于 t,并且 t 的字典序至少是 |t|。因此,答案的长度最多为 K。
生成 s 的所有长度至多为 K 的子串。对它们进行排序,唯一它们,并打印第 K 个,其中 N = |S|。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include
using namespace std;
 
void kThLexString(string st, int k, int n)
{
     
    // Set to store the unique substring
    set z;
     
    for(int i = 0; i < n; i++)
    {
         
       // String to create each substring
       string pp;
        
       for(int j = i; j < i + k; j++)
       {
          if (j >= n)
              break;
          pp += st[j];
           
          // Adding to set
          z.insert(pp);
       }
    }
     
    // Converting set into a list
    vector fin(z.begin(), z.end());
     
    // Sorting the strings int the list
    // into lexicographical order
    sort(fin.begin(), fin.end());
 
    // Printing kth substring
    cout << fin[k - 1];
}
 
// Driver code
int main()
{
    string s = "geeksforgeeks";
    int k = 5;
    int n = s.length();
     
    kThLexString(s, k, n);
}
 
// This code is contributed by yatinagg


Java
// Java implementation of
// the above approach
import java.util.*;
class GFG{
     
public static void kThLexString(String st,
                                int k, int n)
{
    // Set to store the unique substring
    Set z = new HashSet();
     
    for(int i = 0; i < n; i++)
    {
        // String to create each substring
        String pp = "";
     
        for(int j = i; j < i + k; j++)
        {
        if (j >= n)
            break;
        pp += st.charAt(j);
     
        // Adding to set
        z.add(pp);
        }
    }
     
    // Converting set into a list
    Vector fin = new Vector();
    fin.addAll(z);
     
    // Sorting the strings int the list
    // into lexicographical order
    Collections.sort(fin);
     
    // Printing kth substring
    System.out.print(fin.get(k - 1));
}
 
// Driver Code
public static void main(String[] args)
{
    String s = "geeksforgeeks";
    int k = 5;
    int n = s.length();
    kThLexString(s, k, n);
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 implementation of the above approach
def kThLexString(st, k, n):
    # Set to store the unique substring
    z = set()
 
    for i in range(n):
        # String to create each substring
        pp = ""
 
        for j in range(i, i + k):
 
            if (j >= n):
 
                break
 
            pp += s[j]
            # adding to set
            z.add(pp)
 
    # converting set into a list
    fin = list(z)
     
    # sorting the strings int the list
    # into lexicographical order
    fin.sort()
 
    # printing kth substring
    print(fin[k - 1])
 
 
s = "geeksforgeeks"
 
k = 5
 
n = len(s)
kThLexString(s, k, n)


C#
// C# implementation of
// the above approach
using System;
using System.Collections.Generic;
using System.Collections;
 
class GFG{
      
public static void kThLexString(string st,
                                int k, int n)
{
     
    // Set to store the unique substring
    HashSet z = new HashSet();
      
    for(int i = 0; i < n; i++)
    {
         
        // String to create each substring
        string pp = "";
      
        for(int j = i; j < i + k; j++)
        {
            if (j >= n)
                break;
                 
            pp += st[j];
          
            // Adding to set
            z.Add(pp);
        }
    }
     
    // Converting set into a list
    ArrayList fin = new ArrayList();
     
    foreach(string s in z)
    {
        fin.Add(s);
    }
     
    // Sorting the strings int the list
    // into lexicographical order
    fin.Sort();
      
    // Printing kth substring
    Console.Write(fin[k - 1]);
}
  
// Driver Code
public static void Main(string[] args)
{
    string s = "geeksforgeeks";
    int k = 5;
    int n = s.Length;
     
    kThLexString(s, k, n);
}
}
 
// This code is contributed by rutvik_56


Javascript


输出:
eeksf

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