leetcode 211. 添加与搜索单词 - 数据结构设计 解题报告
设计一个支持以下两种操作的数据结构:
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. 添加与搜索单词 - 数据结构设计 解题报告的更多相关文章
- Java实现 LeetCode 211 添加与搜索单词 - 数据结构设计
211. 添加与搜索单词 - 数据结构设计 设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则 ...
- [LeetCode] 211. 添加与搜索单词 - 数据结构设计
题目链接:https://leetcode-cn.com/problems/add-and-search-word-data-structure-design/ 题目描述: 设计一个支持以下两种操作的 ...
- Leetcode 211.添加与搜索单词
添加与搜索单词 设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字 ...
- 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...
- 211 Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
设计一个支持以下两个操作的数据结构:void addWord(word)bool search(word)search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a-z . ...
- [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 ...
- Leetcode211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
设计一个支持以下两种操作的数据结构: void addWord(word) bool search(word) search(word) 可以搜索文字或正则表达式字符串,字符串只包含字母 . 或 a- ...
- [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 ...
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
随机推荐
- nodejs 的一些PHP函数库
http://locutus.io/php/ nodejs 的一些PHP函数库 PHP extensions in JavaScript array array_change_key_case arr ...
- Java 文件切割工具类
Story: 发送MongoDB 管理软件到公司邮箱,工作使用. 1.由于公司邮箱限制附件大小,大文件无法发送,故做此程序用于切割大文件成多个小文件,然后逐个发送. 2.收到小文件之后,再重新组合成原 ...
- 第49章 在SRAM中调试代码—零死角玩转STM32-F429系列
第49章 在SRAM中调试代码 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fir ...
- map集合修改其中元素 去除Map集合中所有具有相同值的元素 Properties长久保存的流操作 两种用map记录单词或字母个数的方法
package com.swift.lianxi; import java.util.HashMap; import java.util.Iterator; import java.util.Map; ...
- ios xmppFramework框架的导入步骤和介绍
一个将要开发xmpp的项目,建议在项目刚创建就导入框架,这样可以避免一些自己操作失误造成不必要的损失. xmpp中最常用的框架就是 xmppFrameWork 第一种方法直接拖 1> 拖入文件夹 ...
- udp重发java实现
最近在处理框架通讯方面的问题,通过积累的开发经验,其实在很多情况(尤其是实时大数据量),udp是占有很多优势的:不需要连接,只管发送,理论上要快很多; 另外在穿墙上占有很大优势: 但是最大的一个问题就 ...
- 获取父窗口元素或者获取iframe中的元素(相同域名下)
jquery方法 在父窗口中获取iframe中的元素 //方法1 $("#iframe的ID").contents().find("iframe中的元素"); ...
- java动态返回一个大对象里多个小对象map返回,el表达式用c:set拼接
基于堆内存,先把map放到返回值里 Map _map=new HashMap(); modelAndView.addObject("pledgeInsurance",_map);/ ...
- python中的集合内置方法小结
#!/usr/local/bin/python3 # -*- coding:utf-8 -*- #集合性质:需要传入一个list,且不含重复的元素,无序 list_1=[1,2,1,4,5,8,3,4 ...
- 准备篇(三)Makefile
Makefile 也是蛮多的, 嵌入式的Makefile也是很重要的,所以单独开一个分支.