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

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. 为什么实例没有prototype属性?什么时候对象会有prototype属性呢?

    为什么实例没有prototype属性?什么时候对象会有prototype属性呢? javascript loudou 1月12日提问 关注 9 关注 收藏 6 收藏,554 浏览 问题对人有帮助,内容 ...

  2. 第十五章 函数————函数的递归、生成器send 、匿名函数

    1.生成器send方法 send的工作原理 1.send发生信息给当前停止的yield 2.再去调用__next__()方法,生成器接着往下指向,返回下一个yield值并停止 例: persons=[ ...

  3. php curl使用总结(一)

    今天和第三方支付做对接的时候,在本地用wamp(php版本5.4.14)运行他们的支付demo的时候,报了一个错误.loadXML函数中不能传空值.排查代码的时候,发现他们用了curl,我以前也接触过 ...

  4. linux下Mycat的安装配置

    1.下载Mycat Linux版:下载链接 2.通过SSH直连工具把安装包丢到linux:/usr/local/ 3.解压安装Mycat 4.配置环境 5.使配置文件生效

  5. 基于mybatis设计简单信息管理系统1

    驼峰式命名法 骆驼式命名法就是当变量名或函数名是由一个或多个单词连结在一起,而构成的唯一识别字时,第一个单词以小写字母开始:第二个单词的首字母大写或每一个单词的首字母都采用大写字母,例如:myFirs ...

  6. MySQL5.6基于mysql-proxy实现读写分离

    已经搭建好MySQL主从架构 10.205.22.185 #mysql-proxy 10.205.22.186 #master 10.205.22.187 #slave 1.安装mysql-proxy ...

  7. 如何设置 html 中 select 标签不可编辑、只读

    转载自: https://blog.csdn.net/hjm4702192/article/details/33729767 1. <select style="width:195px ...

  8. 统计寄存器AX中1 的个数

    ;==================================== ; 统计寄存器AX中1 的个数 DATAS segment DATAS ends CODES segment START: ...

  9. Linq to SQL八大子句

    查询数据库中的数据 from- in子句 指定查询操作的数据源和范围变量 select子句 指定查询结果的类型和表现形式 where子句 筛选元素的逻辑条件,一般由逻辑运算符组成 group- by子 ...

  10. linux防火墙的管理和策略控制

    iptables 一:IPtables防火墙的简介 IPTABLES 是与最新的 3.5 版本 Linux 内核集成的 IP 信息包过滤系统.如果 Linux 系统连接到因特网或 LAN.服务器或连接 ...