字串中字母表示的數字被打亂


在今天的文章中,我們將深入探討一個與 C++ 中字串操作相關的獨特問題。這個問題是“給定字串中字母表示的數字被打亂”。這個問題可以作為提高您 C++ 中字串操作和資料結構技能的絕佳練習。

問題陳述

給定一個字串,任務是識別其字母表示在字串中被打亂的數字。例如,如果輸入字串是“oentow”,它包含數字 2(t、w、o)和 1(o、n、e)的被打亂的表示。

C++ 解決方案方法

為了解決這個問題,我們將利用 C++ 中的雜湊表或無序對映,它將儲存字串中字母的頻率。然後,我們將此頻率對映與每個數字的字母表示的預定義對映進行比較。如果可以從輸入字串中形成一個數字的表示,我們將輸出該數字。

示例

以下是解決問題的程式:

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

// Array of digit representations
char* digitRepresentations[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

// Function to generate frequency map of characters in a string
void generateFrequencyMap(char* str, int freqMap[]) {
   int len = strlen(str);
   for (int i = 0; i < len; i++) {
      freqMap[str[i] - 'a']++;
   }
}

int main() {
   char input[] = "oentow";
   int strFreqMap[26] = {0};  // Frequency map for characters in the input string
   generateFrequencyMap(input, strFreqMap);
    
   printf("The jumbled digits in the string are: ");
   for (int i = 0; i < 10; i++) {
      int digitFreqMap[26] = {0};  // Frequency map for characters in the digit representation
      generateFrequencyMap(digitRepresentations[i], digitFreqMap);
        
      int canFormDigit = 1;
      for (int j = 0; j < 26; j++) {
         if (strFreqMap[j] < digitFreqMap[j]) {
            canFormDigit = 0;
            break;
         }
      }
        
      if (canFormDigit) {
         printf("%d ", i);
      }
   }

   return 0;
}

輸出

The jumbled digits in the string are: 1 2 
#include <iostream>
#include <unordered_map>
#include <vector>

// Array of digit representations
std::string digitRepresentations[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

std::unordered_map<char, int> generateFrequencyMap(std::string str) {
   std::unordered_map<char, int> freqMap;
   for (char c : str) {
      freqMap[c]++;
   }
   return freqMap;
}

std::vector<int> findJumbledDigits(std::string str) {
   std::unordered_map<char, int> strFreqMap = generateFrequencyMap(str);
   std::vector<int> digits;
   
   for (int i = 0; i < 10; i++) {
      std::unordered_map<char, int> digitFreqMap = generateFrequencyMap(digitRepresentations[i]);
      bool canFormDigit = true;
   
      for (auto pair : digitFreqMap) {
         if (strFreqMap[pair.first] < pair.second) {
            canFormDigit = false;
            break;
         }
      }
   
      if (canFormDigit) {
         digits.push_back(i);
      }
   }

   return digits;
}

int main() {
   std::string input = "oentow";
   std::vector<int> digits = findJumbledDigits(input);
   
   std::cout << "The jumbled digits in the string are: ";
   for (int digit : digits) {
      std::cout << digit << " ";
   }

   return 0;
}

輸出

The jumbled digits in the string are: 1 2 
import java.util.HashMap;
import java.util.Map;

public class JumbledDigitsProgram {
   // Map of digit representations
   static Map<Integer, String> digitRepresentations = new HashMap<>();
   static {
      digitRepresentations.put(0, "zero");
      digitRepresentations.put(1, "one");
      digitRepresentations.put(2, "two");
      digitRepresentations.put(3, "three");
      digitRepresentations.put(4, "four");
      digitRepresentations.put(5, "five");
      digitRepresentations.put(6, "six");
      digitRepresentations.put(7, "seven");
      digitRepresentations.put(8, "eight");
      digitRepresentations.put(9, "nine");
   }

   // Function to generate frequency map of characters in a string
   static int[] generateFrequencyMap(String str) {
      int[] freqMap = new int[26];
      for (char c : str.toCharArray()) {
         freqMap[c - 'a']++;
      }
      return freqMap;
   }

   public static void main(String[] args) {
      String input = "oentow";
      int[] strFreqMap = generateFrequencyMap(input);

      System.out.print("The jumbled digits in the string are: ");
      for (int i = 0; i < 10; i++) {
         int[] digitFreqMap = generateFrequencyMap(digitRepresentations.get(i));
         boolean canFormDigit = true;
            
         for (int j = 0; j < 26; j++) {
            if (strFreqMap[j] < digitFreqMap[j]) {
               canFormDigit = false;
               break;
            }
         }

         if (canFormDigit) {
            System.out.print(i + " ");
         }
      }
   }
}

輸出

The jumbled digits in the string are: 1 2 
# Dictionary of digit representations
digit_representations = {
   0: "zero", 1: "one", 2: "two", 3: "three", 4: "four",
   5: "five", 6: "six", 7: "seven", 8: "eight", 9: "nine"
}

# Function to generate frequency map of characters in a string
def generate_frequency_map(s):
   freq_map = [0] * 26
   for c in s:
      freq_map[ord(c) - ord('a')] += 1
   return freq_map

if __name__ == "__main__":
   input_str = "oentow"
   str_freq_map = generate_frequency_map(input_str)
    
   print("The jumbled digits in the string are:", end=" ")
   for i in range(10):
      digit_freq_map = generate_frequency_map(digit_representations[i])
      can_form_digit = all(str_freq_map[ord(c) - ord('a')] >= digit_freq_map[ord(c) - ord('a')] for c in digit_representations[i])
        
      if can_form_digit:
         print(i, end=" ")

輸出

The jumbled digits in the string are: 1 2 

帶測試用例的解釋

讓我們考慮字串“oentow”。

當此字串傳遞給 findJumbledDigits 函式時,它首先為字串生成一個頻率對映:{'o': 2, 'e': 1, 'n': 1, 't': 1, 'w': 1}。

然後,對於從 0 到 9 的每個數字,它生成數字的字母表示的頻率對映,並檢查是否可以從字串的頻率對映中形成此對映。

數字 1 的表示“one”具有頻率對映 {'o': 1, 'n': 1, 'e': 1},數字 2 的表示“two”具有頻率對映 {'t': 1, 'w': 1, 'o': 1}。

這兩個都可以從字串的頻率對映中形成,因此我們將這些數字新增到結果中。

最後,它輸出結果:“字串中被打亂的數字是:1 2”。

結論

這個問題展示了我們如何使用頻率對映來解決 C++ 中複雜的字串操作問題。這是一個練習字串和資料結構處理技能的好問題。繼續練習此類問題以提高您的 C++ 編碼技能。

更新於: 2023-10-16

233 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.