二進位制字串的字典序排名


在本文中,我們將探討一個涉及二進位制字串和字典序排序的有趣問題。我們的任務是找到給定二進位制字串的字典序排名。我們將演示我們的解決方案,這是一種以效率和靈活性而聞名的流行程式語言。

理解字典序排序

字典序排序(也稱為字母順序或詞典順序)指的是根據其組成字母的字母順序排列單詞。

問題陳述

給定一個二進位制字串,我們需要確定它在其所有排列中的字典序排名。字串的字典序排名是該字串的所有排列在字典序排列時在其集合中的位置。

解決方案方法

我們的方法包括以下關鍵步驟:

  • 初始化計數器 - 初始化一個計數器來儲存二進位制字串中'1'的數量。

  • 排名計算 - 從左到右迭代二進位制字串。如果當前字元是'1',則使用組合公式計算其排名,為每個後續的'1'遞減計數器。

  • 返回結果 - 結果將是二進位制字串的字典序排名。

實現

示例

以下程式概述瞭解決方案:

#include <stdio.h>
#include <string.h>

// Function to calculate factorial
int fact(int n) {
   int res = 1;
   for (int i = 1; i <= n; i++)
      res *= i;
   return res;
}

// Function to find lexicographic rank of a binary string
int lexRank(const char* str) {
   int rank = 1;
   int onesCount = 0;
   for (int i = 0; str[i] != '\0'; i++) {
      if (str[i] == '1') {
         onesCount++;
      }
   }

   for (int i = 0; str[i] != '\0'; i++) {
      if (str[i] == '1') {
         onesCount--;
         rank += fact(onesCount);
      }
   }

   return rank;
}

int main() {
   const char* str = "110";

   int result = lexRank(str);
   printf("Lexicographic rank of the binary string: %d\n", result);

   return 0;
}

輸出

Lexicographic rank of the binary string: 3
#include <bits/stdc++.h>
using namespace std;

// Function to calculate factorial
int fact(int n) {
   int res = 1;
   for (int i = 1; i <= n; i++)
      res *= i;
   return res;
}

// Function to find lexicographic rank of a binary string
int lexRank(string str) {
   int rank = 1;
   int onesCount = count(str.begin(), str.end(), '1');
   
   for (char c : str) {
      if (c == '1') {
         onesCount--;
         rank += fact(onesCount);
      }
   }
   
   return rank;
}

int main() {
   string str = "110";
   
   int result = lexRank(str);
   cout << "Lexicographic rank of the binary string: " << result << endl;
   
   return 0;
}

輸出

Lexicographic rank of the binary string: 3
public class Main {
   // Function to calculate factorial
   static int fact(int n) {
      int res = 1;
      for (int i = 1; i <= n; i++)
         res *= i;
      return res;
   }

   // Function to find lexicographic rank of a binary string
   static int lexRank(String str) {
      int rank = 1;
      int onesCount = 0;
      for (int i = 0; i < str.length(); i++) {
         if (str.charAt(i) == '1') {
            onesCount++;
         }
      }

      for (int i = 0; i < str.length(); i++) {
         if (str.charAt(i) == '1') {
            onesCount--;
            rank += fact(onesCount);
         }
      }

      return rank;
   }

   public static void main(String[] args) {
      String str = "110";

      int result = lexRank(str);
      System.out.println("Lexicographic rank of the binary string: " + result);
   }
}

輸出

Lexicographic rank of the binary string: 3
def fact(n):
   res = 1
   for i in range(1, n+1):
      res *= i
   return res

def lex_rank(s):
   rank = 1
   ones_count = s.count('1')
   
   for c in s:
      if c == '1':
         ones_count -= 1
         rank += fact(ones_count)
   
   return rank

if __name__ == "__main__":
   str = "110"
   result = lex_rank(str)
   print("Lexicographic rank of the binary string:", result)

輸出

Lexicographic rank of the binary string: 3

解釋

考慮二進位制字串:

str = "110"

這個二進位制字串的排列是:"011","101","110"。按字典序排列,這些排列是:"011","101","110"。

二進位制字串"110"的排名是3,這是我們程式的輸出。

結論

查詢二進位制字串的字典序排名的問題是一個引人入勝的問題,它建立在我們對二進位制字串、排列和字典序的理解之上。這個用C、C++、Java和Python實現的解決方案演示了我們如何使用基本的程式設計結構有效地解決它。

更新於:2023年10月23日

393 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.