我输入了一个字符串类型的句子。
std::string message;
std::getline(std::cin, message);
输入一个句子后,我使用 if 语句将字符串转换为“摩尔斯电码”:
int length = message.length();
for(int i = 0; i < length;i++) //to loop in the message
{
if(message[i] == 'A')
cout << "-.";//and the rest for 'b','c','d'....'z'
}
我如何获取输入字符串的摩尔斯电码并对其进行解码。例如: 如果在摩尔斯电码中是“.-”,则显示“A”,如果消息中是“-...”,则显示“B”。
最佳答案
以这种方式使用二叉树 - 根为空 (NULL)。 每个 child 都有一个字符“-”“。” . 这样你就可以将摩尔斯电码解码到树中。 现在而不是 NULL 放在最后把你应该在最后得到的字符。 树应该是这样的:
root / \ '-' '.' \ '.' \ 'A'
等 现在当 n = 树的大小时,您可以在 O(lg n) 中找到字符。
关于c++ - 如何在字符串中搜索 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11916550/