Add and Search Word - Data structure design

Design a data structure that supports the following two operations:

void addWord(word)
bool search(word)

search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.

For example:

addWord("bad")
addWord("dad")
addWord("mad")
search("pad") -> false
search("bad") -> true
search(".ad") -> true
search("b..") -> true

比较有意思的题目,主要涉及到通配符'.'的处理,总体思路还是用trie.但是和普通trie上的完全match不同,这里需要在'.'时候做遍历,涉及到backtracking的处理.

代码如下:

class TrieNode(object):
    def __init__(self):
        self.children = {}
        self.hasword = False

class WordDictionary(object):
    def __init__(self):
        """
        initialize your data structure here.
        """
        self.root = TrieNode()
        
    def addWord(self, word):
        """
        Adds a word into the data structure.
        :type word: str
        :rtype: void
        """
        node = self.root
        for i in word:
            if not node.children.get(i):
                node.children[i] = TrieNode()
            node = node.children[i]
        node.hasword = True
        
    def search(self, word):
        """
        Returns if the word is in the data structure. A word could
        contain the dot character '.' to represent any one letter.
        :type word: str
        :rtype: bool
        """
        node = self.root
        return self.dfs(word, 0, node)
            
    def dfs(self, word, i, node):
        if i == len(word) - 1:
            if word[i] != '.':
                if node.children.get(word[i]):
                    return node.children[word[i]].hasword
                else:
                    return False
            else:
                for j in node.children.values():
                    if j.hasword:
                        return True
                return False
        if word[i] == '.':
            for j in node.children.values():
                if self.dfs(word, i+1, node): # backtracking 没有的话,会退回
                    return True
            return False
        else:
            if node.children.get(word[i]):
                return self.dfs(word, i+1, node.children[word[i]])
            else:
                return False
            

 优化版本:

class TrieNode(object):
    def __init__(self):
        self.children = {}
        self.hasword = False

class WordDictionary(object):

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.root = TrieNode()

    def addWord(self, word):
        """
        Adds a word into the data structure.
        :type word: str
        :rtype: void
        """
        node = self.root
        for c in word:
            if c not in node.children:
                node.children[c] = TrieNode()
            node = node.children[c]
        node.hasword = True    
        
    def search(self, word):
        """
        Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter.
        :type word: str
        :rtype: bool
        """
        node = self.root
        return self.dfs(node, word, 0)
        
    def dfs(self, node, word, i):
        if i == len(word):
            return node.hasword
        if word[i] == '.':
            for c in node.children:
                if self.dfs(node.children[c], word, i+1):
                    return True
            return False    
        else:
            if word[i] in node.children:
                return self.dfs(node.children[word[i]], word, i+1)
            else:
                return False

add的复杂度是单词的长度,search的复杂度最大是遍历树的所有子结点

转载于:https://www.cnblogs.com/sherylwang/p/5640713.html

Direct sequence spread spectrum (DSSS) is a modulation technique used in wireless communication systems to enhance the resistance to interference and increase the security of the communication. In this technique, the data signal is spread over a wider frequency band by multiplying it with a code sequence. The receiver then uses the same code sequence to despread the received signal and recover the original data. In order to achieve synchronization between the transmitter and receiver in a DSSS system, the receiver must synchronize its code sequence with the transmitter's code sequence. This synchronization can be achieved by using a synchronization code or by using a tracking loop. Matlab is a powerful tool for analyzing and simulating the performance of DSSS systems. It provides a wide range of functions and tools for implementing and testing DSSS systems. To analyze and simulate the synchronization performance of a DSSS system in Matlab, the following steps can be followed: 1. Design the DSSS system: This involves designing the spreading and despreading codes, the modulation scheme, and the receiver structure. 2. Generate the transmitted signal: Use Matlab functions to generate the transmitted signal by spreading the data signal with the spreading code. 3. Add noise: Add white Gaussian noise to the transmitted signal to simulate the effects of interference. 4. Implement the receiver: Implement the receiver structure in Matlab, including the synchronization code or tracking loop. 5. Perform synchronization: Use Matlab functions to perform synchronization between the transmitter and receiver. 6. Evaluate performance: Evaluate the performance of the DSSS system by calculating the bit error rate (BER) and signal-to-noise ratio (SNR) for different levels of interference and synchronization. 7. Optimize performance: Use Matlab tools to optimize the performance of the DSSS system by adjusting parameters such as the spreading code length, modulation scheme, and receiver structure. Overall, Matlab provides a powerful and flexible platform for analyzing and simulating the performance of DSSS systems, including the synchronization performance. By following the above steps and using Matlab's tools and functions, it is possible to design and optimize a robust and efficient DSSS system for wireless communication applications.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值