📜  字符串在其所有子字符串中的词法等级

📅  最后修改于: 2021-05-04 23:38:31             🧑  作者: Mango

给定字符串str ,任务是在按字典顺序排列的所有子字符串中找到给定字符串的等级。

例子:

方法:请按照以下步骤解决问题:

  1. 初始化长度为26的向量的数组arr [] ,以存储字符串存在的字符的索引,并将其排名为0。
  2. 存储每个字符的索引。索引将被存储在ARR [0],对于b,编曲[1]将存储其所有指数等。
  3. 遍历存储在数组ARR []到其比字符串S的第一字符较小字符的每个索引。
  4. 对于每个索引i ,从该索引开始的子字符串总数为N – i 。加N – i进行排名,因为具有这些索引的所有字符都较小。
  5. 现在,遍历之后,存储所有从S的第一个字符开始的子字符串,并按字典顺序对这些子字符串进行排序。
  6. 遍历排序的子字符串,并将每个子字符串与字符串S进行比较,并递增等级,直到找到等于S的子字符串。
  7. 打印等级+1,以获取给定字符串的等级。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find lexiographic rank
// of string among all its substring
int lexiographicRank(string s)
{
    // Length of string
    int n = s.length();
 
    vector alphaIndex[26];
 
    // Traverse the given string
    // and store the indices of
    // each character
    for (int i = 0; i < s.length(); i++) {
 
        // Extract the index
        int x = s[i] - 'a';
 
        // Store it in the vector
        alphaIndex[x].push_back(i);
    }
 
    // Traverse the aphaIndex array
    // lesser than the index of first
    // character of given string
    int rank = 0;
 
    for (int i = 0; i < 26
                    && 'a' + i < s[0];
         i++) {
 
        // If alphaIndex[i] size exceeds 0
        if (alphaIndex[i].size() > 0) {
 
            // Traverse over the indices
            for (int j = 0;
                 j < alphaIndex[i].size(); j++) {
 
                // Add count of substring
                // equal to n - alphaIndex[i][j]
                rank = rank
                       + (n
                          - alphaIndex[i][j]);
            }
        }
    }
 
    vector str;
 
    for (int i = 0;
         i < alphaIndex[s[0] - 'a'].size();
         i++) {
 
        // Store all substrings in a vector
        // str starting with the first
        // character of the given string
        string substring;
 
        int j = alphaIndex[s[0] - 'a'][i];
 
        for (; j < n; j++) {
 
            // Insert the current
            // character to substring
            substring.push_back(s[j]);
 
            // Store the substring formed
            str.push_back(substring);
        }
    }
 
    // Sort the substring in the
    // lexicographical order
    sort(str.begin(), str.end());
 
    // Find the rank of given string
    for (int i = 0; i < str.size(); i++) {
 
        if (str[i] != s) {
 
            // increase the rank until
            // the given string is same
            rank++;
        }
 
        // If substring is same as
        // the given string
        else {
            break;
        }
    }
 
    // Add 1 to rank of
    // the given string
    return rank + 1;
}
 
// Driver Code
int main()
{
    // Given string
    string str = "enren";
 
    // Function Call
    cout << lexiographicRank(str);
 
    return 0;
}


Java
// Java program for
// the above approach
import java.util.*;
class GFG{
 
// Function to find lexiographic rank
// of String among all its subString
static int lexiographicRank(char []s)
{
  // Length of String
  int n = s.length;
 
  Vector []alphaIndex = new Vector[26];
  for (int i = 0; i < alphaIndex.length; i++)
    alphaIndex[i] = new Vector();
   
  // Traverse the given String
  // and store the indices of
  // each character
  for (int i = 0; i < s.length; i++)
  {
    // Extract the index
    int x = s[i] - 'a';
 
    // Store it in the vector
    alphaIndex[x].add(i);
  }
 
  // Traverse the aphaIndex array
  // lesser than the index of first
  // character of given String
  int rank = 0;
 
  for (int i = 0; i < 26 &&
           'a' + i < s[0]; i++)
  {
    // If alphaIndex[i] size exceeds 0
    if (alphaIndex[i].size() > 0)
    {
      // Traverse over the indices
      for (int j = 0;
               j < alphaIndex[i].size();
               j++)
      {
        // Add count of subString
        // equal to n - alphaIndex[i][j]
        rank = rank + (n - alphaIndex[i].get(j));
      }
    }
  }
 
  Vector str = new Vector();
 
  for (int i = 0;
           i < alphaIndex[s[0] - 'a'].size();
           i++)
  {
    // Store all subStrings in a vector
    // str starting with the first
    // character of the given String
    String subString = "";
 
    int j = alphaIndex[s[0] - 'a'].get(i);
 
    for (; j < n; j++)
    {
      // Insert the current
      // character to subString
      subString += (s[j]);
 
      // Store the subString formed
      str.add(subString);
    }
  }
 
  // Sort the subString in the
  // lexicographical order
  Collections.sort(str);
 
  // Find the rank of given String
  for (int i = 0; i < str.size(); i++)
  {
    if (!str.get(i).equals(String.valueOf(s)))
    {
      // increase the rank until
      // the given String is same
      rank++;
    }
 
    // If subString is same as
    // the given String
    else
    {
      break;
    }
  }
 
  // Add 1 to rank of
  // the given String
  return rank + 1;
}
 
// Driver Code
public static void main(String[] args)
{
  // Given String
  String str = "enren";
 
  // Function Call
  System.out.print(lexiographicRank(str.toCharArray()));
}
}
 
// This code is contributed by shikhasingrajput


Python3
# Python3 program for the above approach
 
# Function to find lexiographic rank
# of among all its substrring
def lexiographicRank(s):
     
    # Length of strring
    n = len(s)
 
    alphaIndex = [[] for i in range(26)]
 
    # Traverse the given strring
    # and store the indices of
    # each character
    for i in range(len(s)):
 
        # Extract the index
        x = ord(s[i]) - ord('a')
 
        # Store it in the vector
        alphaIndex[x].append(i)
 
    # Traverse the aphaIndex array
    # lesser than the index of first
    # character of given strring
    rank = -1
 
    for i in range(26):
        if ord('a') + i >= ord(s[0]):
            break
 
        # If alphaIndex[i] size exceeds 0
        if len(alphaIndex[i]) > 0:
 
            # T raverse over the indices
            for j in range(len(alphaIndex[i])):
 
                # Add count of substrring
                # equal to n - alphaIndex[i][j]
                rank = rank + (n - alphaIndex[i][j])
                 
    # print(rank)
    strr = []
 
    for i in range(len(alphaIndex[ord(s[0]) - ord('a')])):
 
        # Store all substrrings in a vector
        # strr starting with the first
        # character of the given strring
        substrring = []
 
        jj = alphaIndex[ord(s[0]) - ord('a')][i]
 
        for j in range(jj, n):
 
            # Insert the current
            # character to substrring
            substrring.append(s[j])
 
            # Store the subformed
            strr.append(substrring)
 
    # Sort the subin the
    # lexicographical order
    strr = sorted(strr)
 
    # Find the rank of given strring
    for i in range(len(strr)):
        if (strr[i] != s):
 
            # Increase the rank until
            # the given is same
            rank += 1
 
        # If subis same as
        # the given strring
        else:
            break
 
    # Add 1 to rank of
    # the given strring
    return rank + 1
 
# Driver Code
if __name__ == '__main__':
     
    # Given strring
    strr = "enren"
 
    # Function call
    print(lexiographicRank(strr))
 
# This code is contributed by mohit kumar 29


C#
// C# program for
// the above approach
using System;
using System.Collections.Generic;
class GFG{
 
// Function to find lexiographic rank
// of String among all its subString
static int lexiographicRank(char []s)
{
  // Length of String
  int n = s.Length;
 
  List []alphaIndex = new List[26];
  for (int i = 0; i < alphaIndex.Length; i++)
    alphaIndex[i] = new List();
 
  // Traverse the given String
  // and store the indices of
  // each character
  for (int i = 0; i < s.Length; i++)
  {
    // Extract the index
    int x = s[i] - 'a';
 
    // Store it in the vector
    alphaIndex[x].Add(i);
  }
 
  // Traverse the aphaIndex array
  // lesser than the index of first
  // character of given String
  int rank = 0;
 
  for (int i = 0; i < 26 &&
           'a' + i < s[0]; i++)
  {
    // If alphaIndex[i] size exceeds 0
    if (alphaIndex[i].Count > 0)
    {
      // Traverse over the indices
      for (int j = 0;
               j < alphaIndex[i].Count; j++)
      {
        // Add count of subString
        // equal to n - alphaIndex[i,j]
        rank = rank + (n - alphaIndex[i][j]);
      }
    }
  }
 
  List str = new List();
 
  for (int i = 0;
           i < alphaIndex[s[0] - 'a'].Count; i++)
  {
    // Store all subStrings in a vector
    // str starting with the first
    // character of the given String
    String subString = "";
 
    int j = alphaIndex[s[0] - 'a'][i];
 
    for (; j < n; j++)
    {
      // Insert the current
      // character to subString
      subString += (s[j]);
 
      // Store the subString formed
      str.Add(subString);
    }
  }
 
  // Sort the subString in the
  // lexicographical order
  str.Sort();
 
  // Find the rank of given String
  for (int i = 0; i < str.Count; i++)
  {
    if (!str[i].Equals(String.Join("", s)))
    {
      // increase the rank until
      // the given String is same
      rank++;
    }
 
    // If subString is same as
    // the given String
    else
    {
      break;
    }
  }
 
  // Add 1 to rank of
  // the given String
  return rank + 1;
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given String
  String str = "enren";
 
  // Function Call
  Console.Write(lexiographicRank(str.ToCharArray()));
}
}
 
// This code is contributed by 29AjayKumar


输出:
7







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