804. Unique Morse Code Words

本文介绍了一种算法,用于计算不同单词转换为摩斯电码后的唯一组合数量。通过映射每个字母到对应的摩斯电码,该算法能够找出给定单词列表中不同的摩斯电码表示。

804. Unique Morse Code Words


题目

Leetcode题目

International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: “a” maps to “.-“, “b” maps to “-…”, “c” maps to “-.-.”, and so on.

For convenience, the full table for the 26 letters of the English alphabet is given below:

[".-","-...","-.-.","-..",".","..-.",
 "--.","....","..",".---","-.-",".-..",
 "...","-","..-","...-",".--","-..-",
 "-.--","--.."]

Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, “cab” can be written as “-.-.-….-“, (which is the concatenation “-.-.” + “-…” + “.-“). We’ll call such a concatenation, the transformation of a word.

Return the number of different transformations among all words we have.

Input: words = ["gin", "zen", "gig", "msg"]
Output: 2
Explanation: 
The transformation of each word is:
"gin" -> "--...-."
"zen" -> "--...-."
"gig" -> "--...--."
"msg" -> "--...--."

There are 2 different transformations, “–…-.” and “–…–.”.

Note:

  • The length of words will be at most 100.
  • Each words[i] will have length in range [1, 12].
  • words[i] will only consist of lowercase letters.

解决

1.使用map来一一对应相应的摩斯密码
【n为words的size,m为words中最长的string的length】

  • 时间复杂度:O(n * m)
  • 空间复杂度:O(n)
// First Solution (Runtime:7 ms)
class Solution {
public:
    int uniqueMorseRepresentations(vector<string>& words) {
        // store the International Morse Code
        map<char, string> table;
        set<string> translate;
        // initialize table
        initTable(table);
        int num = words.size();
        for (int i = 0; i < num; i++) {
            int len = words[i].length();
            string temp = "";
            for (int j = 0; j < len; j++) {
                temp += table[words[i][j]];
            }
            translate.insert(temp);
        }
        return translate.size();
    }

private:
    void initTable(map<char, string>& table) {
        table['a'] = ".-";
        table['b'] = "-...";
        table['c'] = "-.-.";
        table['d'] = "-..";
        table['e'] = ".";
        table['f'] = "..-.";
        table['g'] = "--.";
        table['h'] = "....";
        table['i'] = "..";
        table['j'] = ".---";
        table['k'] = "-.-";
        table['l'] = ".-..";
        table['m'] = "--";
        table['n'] = "-.";
        table['o'] = "---";
        table['p'] = ".--.";
        table['q'] = "--.-";
        table['r'] = ".-.";
        table['s'] = "...";
        table['t'] = "-";
        table['u'] = "..-";
        table['v'] = "...-";
        table['w'] = ".--";
        table['x'] = "-..-";
        table['y'] = "-.--";
        table['z'] = "--..";
    }
};

2.利用vector和下标一一对应
【n为words的size,m为words中最长的string的length】

  • 时间复杂度:O(n * m)
  • 空间复杂度:O(n)
// Second Solution (Runtime:6 ms)
class Solution {
public:
    int uniqueMorseRepresentations(vector<string>& words) {
        unordered_set<string> translate; // or set<string> translate;
        // store the International Morse Code
        vector<string> table = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
        int num = words.size();
        for (int i = 0; i < num; i++) {
            int len = words[i].length();
            string temp = "";
            for (int j = 0; j < len; j++) {
                temp += table[words[i][j] - 'a'];
            }
            if (translate.find(temp) == translate.end()) {
                translate.insert(temp);
                // if use set, just use 'translate.insert(temp);' directly
            }
        }
        return translate.size();
    }
};

虽然第二种方法的时间复杂度和空间复杂度上没有太大的变化,但是第二种方法运行较第一种方法快的原因是少了“initTable()”函数的调用,并且第二种方法使用的是unordered_set,这样可以减少插入set时候进行的排序所耽误的时间。

<think>嗯,用户想在GNU Radio中添加一个Morse Code Vector Source。首先,我得回忆一下GNU Radio的组件结构。GNU Radio通常使用块(blocks)来处理信号,所以用户可能需要创建一个自定义的块来生成莫尔斯码的向量源。 首先,用户需要了解如何创建自定义的GNU Radio块。这可能涉及到使用gr_modtool,这是GNU Radio自带的模块创建工具。我应该先检查gr_modtool的使用方法,确保步骤正确。比如,使用`gr_modtool newmod`创建新模块,然后添加新的块。 接下来,莫尔斯码的生成逻辑。莫尔斯码由点和划组成,每个字符对应不同的时间序列。用户需要将输入的文本转换为对应的莫尔斯码符号,再将这些符号转换为时间序列的采样点。例如,点可能是一个短脉冲,划是长脉冲,字符之间和单词之间有不同的间隔时间。 然后,关于信号参数,比如采样率、符号速率。假设每个点的时间长度为基本单位,比如1个点=1个单位时间,划就是3个单位,字符间隔是3个单位,单词间隔是7个单位。用户需要根据这些参数生成相应的采样向量。 另外,输入输出类型的问题。作为Vector Source,可能输出的是浮点型或字节型的向量,每个元素代表一个采样点的幅度。可能需要使用Throttle块来控制数据流的速度,避免占用过多CPU资源。 测试和验证也是关键。用户可能需要使用QT GUI Time Sink来查看生成的波形是否正确,或者连接到音频输出块来听莫尔斯码的声音。调试过程中可能需要调整时间参数或符号映射。 还有可能遇到的常见问题,比如块同步问题、采样率不匹配,或者生成的向量长度不正确。需要确保生成的向量长度与GNU Radio的缓冲区大小兼容,避免出现流水线阻塞或数据丢失的情况。 最后,用户可能需要参考GNU Radio的官方文档和社区资源,比如邮件列表或论坛,寻找类似项目的例子或解决方案。如果现有模块库中有类似的模块,用户也可以直接使用或进行修改,而不必从头开始编写。</think>### 如何在GNU Radio中添加Morse Code Vector Source #### 步骤 1:理解需求 Morse Code Vector Source需要实现以下功能: 1. 将输入文本转换为国际莫尔斯码符号序列(如`A->".-"`) 2. 根据符号生成对应时间序列(点=短脉冲,划=长脉冲) 3. 添加字符间/单词间间隔 4. 输出为离散采样值向量 #### 步骤 2:创建自定义模块 使用GNU Radio Companion的`gr_modtool`创建新模块: ```bash gr_modtool newmod morse cd gr-morse gr_modtool add -t sync -l python morse_vector_source ``` #### 步骤 3:实现核心逻辑 在生成的`morse_vector_source_impl.py`中添加代码: ```python MORSE_CODE = { 'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', # ...完整映射表... } def generate_symbols(text, dot_duration=0.1, sample_rate=8000): samples = [] for char in text.upper(): code = MORSE_CODE.get(char, '') for symbol in code: duration = dot_duration if symbol == '.' else 3*dot_duration samples += [1.0]*int(duration*sample_rate) samples += [0.0]*int(dot_duration*sample_rate) # 符号内间隔 samples += [0.0]*int(3*dot_duration*sample_rate) # 字符间间隔 return samples ``` #### 步骤 4:配置块参数 在`morse_vector_source.block.yml`中定义参数: ```yaml parameters: - id: text label: Input Text dtype: string - id: dot_duration label: Dot Duration (s) dtype: float default: 0.1 ``` #### 步骤 5:测试流程 构建测试流程图: ``` Morse Vector Source -> Throttle -> QT GUI Time Sink -> Audio Sink ``` #### 验证参数示例: - 采样率:8000 Hz - 点持续时间:0.1秒 - 划持续时间:0.3秒 - 字符间隔:0.3秒 - 单词间隔:0.7秒 #### 性能优化建议: 1. 使用预生成查找表加速符号生成 2. 添加流控制机制防止缓冲区溢出 3. 支持实时文本输入接口
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值