字符串中字母表示的数字被打乱
在今天的文章中,我们将深入探讨一个与 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++ 编码技能。
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP