设计一个支持以下两种操作的数据结构:

void addWord(word)
bool search(word)

search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 .a-z. 可以表示任何一个字母。

示例:

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

说明:

你可以假设所有单词都是由小写字母 a-z 组成的。

解题思路

直接用字典树(trie)即可,至于.匹配符直接利用回溯即可。

#include<bits/stdc++.h>

using namespace std;

const int nch = 26;
const int maxn = 200010; static auto x = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
return 0;
}
(); struct Node {
int ch[nch];
bool flag; void reset() {
for(int i = 0; i < nch; ++i) {
ch[i] = -1;
}
flag = false;
}
}; Node tree[maxn]; class WordDictionary {
public:
/** Initialize your data structure here. */
int tot;
WordDictionary() {
tree[tot = 0].reset();
} /** Adds a word into the data structure. */
void addWord(string word) {
int root = 0;
for(auto &chr:word) {
if(tree[root].ch[chr - 'a'] == -1) {
tree[root].ch[chr - 'a'] = ++tot;
tree[tot].reset();
}
root = tree[root].ch[chr - 'a'];
}
tree[root].flag = true;
} /** Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter. */
bool search(string word) {
return _search(word, 0, 0);
} bool _search(string &word, int cur, int root) {
if(cur == word.size() && tree[root].flag)
return true;
if(cur >= word.size() or root == -1)
return false;
if(word[cur] == '.') {
for(int i = 0; i < nch; ++i) {
if(tree[root].ch[i] != -1 && _search(word, cur + 1, tree[root].ch[i]))
return true;
}
return false;
}
if(tree[root].ch[word[cur]-'a'] != -1)
return _search(word, cur + 1, tree[root].ch[word[cur]-'a']);
return false;
}
}; int main() { return 0;
}

leetcode 211. 添加与搜索单词 - 数据结构设计 解题报告的更多相关文章

  1. Java实现 LeetCode 211 添加与搜索单词 - 数据结构设计

    211. 添加与搜索单词 - 数据结构设计 设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则 ...

  2. [LeetCode] 211. 添加与搜索单词 - 数据结构设计

    题目链接:https://leetcode-cn.com/problems/add-and-search-word-data-structure-design/ 题目描述: 设计一个支持以下两种操作的 ...

  3. Leetcode 211.添加与搜索单词

    添加与搜索单词 设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字 ...

  4. 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...

  5. 211 Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计

    设计一个支持以下两个操作的数据结构:void addWord(word)bool search(word)search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z . ...

  6. [Swift]LeetCode211. 添加与搜索单词 - 数据结构设计 | Add and Search Word - Data structure design

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  7. Leetcode211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计

    设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a- ...

  8. [LeetCode] 211. Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  9. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

随机推荐

  1. img适配问题解决方法

    将img放到background中,将background-size设置为100%:只需要适配背景为img的元素的宽和高即可.

  2. python 生成器与迭代器(yield 用法)

    背景 首先,我不会解释这两个名词,我看过很多遍解释,可还是看不懂,还是直接看使用情景吧. 我们以佩波拉契数列为例,当我们不知道迭代器的情况下,我们写出来的代码可能是这样子的: '''这种方式计算fib ...

  3. 使用Git将本地项目上传到码云

    01.码云上创建一个项目 testgit (名字随意) 02.进入本地想要上传的目录,然后使用git bash 03.cd到想要上传的目录 04.$ git init 初始化一个git 本地仓库(项目 ...

  4. 银行卡验证API

    一.银联开放平台 https://open.unionpay.com/tjweb/api/detail?apiSvcId=21 应用场景 综合数据服务平台是银联为接入商户提供的综合数据认证服务接口,目 ...

  5. oracle命令集

    1.安装oracle后,需要导入表数据,操作如下: 连接数据库:sqlplus / as sysdba 创建表空间: create tablespace user_data logging dataf ...

  6. 精读《12 个评估 JS 库你需要关心的事》

    1 引言 作者给出了从 12 个角度全面分析 JS 库的可用性,分别是: 特性. 稳定性. 性能. 包生态. 社区. 学习曲线. 文档. 工具. 发展历史. 团队. 兼容性. 趋势. 下面总结一下作者 ...

  7. python__高级 : 类的__getattribute__ 方法

    在类 里面,其实并没有方法这个东西,所有的东西都保存在属性里面,所谓的调用方法其实是类里面的一个同名属性指向了一个函数(方法),返回的是函数的引用,再用   函数()    这种方式就可以调用它 在调 ...

  8. tcl之控制流-条件运算、条件测试、逻辑表达

  9. Windows下如何安装composer

    相对 来说并不难直接将此文件下载安装即可 1 https://getcomposer.org/Composer-Setup.exe 文件地址由官方提供 https://getcomposer.org/ ...

  10. Oauth2.0协议 http://www.php20.com/forum.php?mod=viewthread&tid=28 (出处: 码农之家)

    概要     OAuth2.0是OAuth协议的下一版本,但不向后兼容OAuth 1.0即完全废止了OAuth1.0. OAuth 2.0关注客户端开发者的简易性.要么通过组织在资源拥有者和HTTP服 ...