📌  相关文章
📜  二进制字符串的字典顺序

📅  最后修改于: 2021-04-17 19:05:32             🧑  作者: Mango

给定长度为N的二进制字符串S ,任务是查找给定字符串的词典顺序。

例子:

天真的方法:最简单的方法是生成长度为N的所有可能的二进制字符串(01组成)并将其存储在数组中。完成后,按字典顺序对字符串数组进行排序,并在数组中打印给定字符串的索引。
时间复杂度: O(2 N )
辅助空间: O(1)

高效的方法:这个想法是通过将每次出现的‘a’替换为0并将‘b’替换为1来生成由01s组成的二进制字符串。因此,列表中的字符串变为:

可以观察到,对于大小为N的字符串,存在(2 N – 2),该给定的字符串之前长度的字符串小于N。使其等于X。现在,可以通过将字符串转换为其十进制等效数字并将其加1来计算其在长度为N的字符串的词典顺序位置。使其等于Y。

下面是上述方法的实现:

C++
//C++ program for the above approach
#include 
using namespace std;
 
// Function to find the rank of a string
void findRank(string s)
{
 
  // Store the length of the string
  int N = s.size();
 
  // Stores its equivalent decimal value
  string bin;
 
  // Traverse the string
  for (int i = 0; i < N; i++)
  {
    if (s[i] == '0')
      bin += "0";
    else
      bin += "1";
  }
   
  // Store the number of strings of
  // length less than N occurring
  // before the given string
  long long X = 1ll<= 0; i--)
  {
    if (bin[i] == '1') 
      res += (val);
 
    val *= 2ll;
  }
 
  long long Y = res;
 
  // Store the rank in answer
  long ans = X + Y - 1;
 
  // Print the answer
  cout << ans;
}
 
// Driver program
int main()
{
  string S = "001";
  findRank(S);
  return 0;
}
 
// This code is contributed by jyoti369.


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
public class Main {
 
    // Function to find the rank of a string
    static void findRank(String s)
    {
        // Store the length of the string
        int N = s.length();
 
        // Stores its equivalent decimal value
        StringBuilder sb = new StringBuilder("");
 
        // Traverse the string
        for (int i = 0; i < N; i++) {
 
            if (s.charAt(i) == '0')
                sb.append('0');
            else
                sb.append('1');
        }
 
        String bin = sb.toString();
 
        // Store the number of strings of
        // length less than N occurring
        // before the given string
        long X = (long)Math.pow(2, N);
 
        // Store the decimal equivalent
        // number of string bin
        long Y = Long.parseLong(bin, 2);
 
        // Store the rank in answer
        long ans = X + Y - 1;
 
        // Print the answer
        System.out.println(ans);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        String S = "001";
        findRank(S);
    }
}


Python3
# Python program for the above approach
 
 
# Function to find the rank of a string
def findRank(s):
   
    # Store the length of the string
    N = len(s);
 
    # Stores its equivalent decimal value
    sb = "";
 
    # Traverse the string
    for i in range(0,N):
 
        if (s[i] == '0'):
            sb += str('0');
        else:
            sb += str('1');
 
    bin = str(sb);
 
    # Store the number of strings of
    # length less than N occurring
    # before the given string
    X = pow(2, N);
 
    # Store the decimal equivalent
    # number of string bin
    Y = int(bin);
 
    # Store the rank in answer
    ans = X + Y - 1;
 
    # Prthe answer
    print(ans);
 
# Driver Code
if __name__ == '__main__':
    S = "001";
    findRank(S);
 
# This code is contributed by shikhasingrajput


C#
// C# program to implement
// the above approach
using System;
 
class GFG
{
   
// Function to find the rank of a String
static void findRank(string s)
{
 
  // Store the length of the String
  int N = s.Length;
 
  // Stores its equivalent decimal value
  string bin = "";
 
  // Traverse the String
  for (int i = 0; i < N; i++)
  {
    if (s[i] == '0')
      bin += "0";
    else
      bin += "1";
  }
   
  // Store the number of string s of
  // length less than N occurring
  // before the given String
  int X = 1<= 0; i--)
  {
    if (bin[i] == '1') 
      res += (val);
 
    val *= 2;
  }
  int Y = res;
 
  // Store the rank in answer
  int ans = X + Y - 1;
 
  // Print the answer
  Console.Write(ans);
}
 
// Driver Code
public static void Main()
{
   string S = "001";
    findRank(S);
}
}
 
// This code is contributed by splevel62.


输出:
8

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