模板
**#include <iostream>
using namespace std;
const int N=100010;
int son[N][26], cnt[N], idx;
char str[N];
//son:子节点 26是因为最多有二十六条边
//idx =0:即是空结点也是根节点 idx为全局变量 会一直加下去
// cnt[] 统计单词出现的次数,以当前字母结尾的单词的个数
//插入单词
void insert (char *str) {
int p = 0;//从根节点开始
for (int i = 0; str[i];i++) {
int u = str[i] - 'a';//映射成0~25
if (!son[p][u])//判断结点是否存在
son[p][u] = ++idx;//不存在创建新结点
p = son[p][u];//移动p指针
}
cnt[p]++;//单词末尾标记单词出现次数
//也就是图中红色五角星★
}
//查询该字符串出现的次数
int query (char *str) {
int p = 0; //从根节点开始
for (int i = 0; str[i];i++) {
int u = str[i] - 'a';
if (!son[p][u])
return 0;//如果该字符串不存在
p = son[p][u];//移动p指针
}
return cnt[p];//返回出现次数 也就是有几个★
}
int main ( ) {
int n;
scanf("%d", &n);
while (n -- ){
char op[2];
scanf("%s%s", op, str);
if (op[0] == 'I')
insert(str);
else
printf("%d", query(str));
}
return 0;
}
**
LeetCode 139. 单词拆分
(1)思路
设计一个长度为 s + 1 的布尔数组,记录 [0,s.size()] 中各个位置的可到达情况。
从左至右依次遍历,针对每个可到达的位置,遍历字典中单词,若该位置的 s 字符串与单词匹配,则 s 中对应单词长度后的位置也可到达。
布尔数组对应的最后一个位置,即 s.size() 的布尔值,即单词是否可拆分。
class Solution {
public:
bool wordBreak(string s, vector<string>& wordDict) {
if (s.empty())
return true;
if (wordDict.empty())
return false;
int len = s.size() + 1, wsize = wordDict.size();
vector<bool>dp(len, false);//记录各个位置是否可到达
dp[0] = true;
for (int i = 0; i < len; i++) {//遍历s -->按照字母单词顺序,依次判断wordDict里面有没有可达的片段
if (dp[i] == true) {//针对已可达的位置进行后续判断
for (int j = 0; j < wsize; j++) {//遍历字典中单词
bool isMatch = true;
for (int k = 0; k < wordDict[j].size(); k++) {//判断单词是否匹配,wordDict[j].size()是以当前字母开头的单词的长度-->wordDict[1].size() "code"可达
if (s[i + k] != wordDict[j][k]) {
isMatch = false;
break;
}
}
if (isMatch)//若单词匹配,对应长度后的位置也可到达,即字典里面有这个单词的某一节
dp[i + wordDict[j].size()] = true;
}
}
}
return dp[s.size()];//最后一个位置能否到达,即单词是否可拆分
}
};
class Solution {
public:
bool wordBreak(string& s, vector<string>& wordDict) {
vector<bool> dp(s.size()+1, false);
dp[0] = true;
for(int i = 0; i < s.size(); i++){
if(!dp[i])
continue;
//隐含条件dp[i]存在则继续以下
for(auto& word : wordDict)
//dp[i] == true && s.substr(i, word.size()) == word
//前面存在&&后面匹配
if(word.size() + i <= s.size() && s.substr(i, word.size()) == word)
dp[i+word.size()] = true;
}
return dp[s.size()];
}
};
图解参考:
https://leetcode.cn/problems/word-break/solution/shou-hui-tu-jie-san-chong-fang-fa-dfs-bfs-dong-tai/
LeetCode 140.
但是本题中,不仅需要判断能否拆分,还要找到所有拆分方式。因此需要保存能够到达 dp[i] 的所有前驱节点 k,相当于建立一棵树,便于之后使用dfs 搜索得到所有可行路径。下面举个例子
作者:liuyvjin
链接:https://leetcode.cn/problems/word-break-ii/solution/by-liuyvjin-6rzk/
来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
LeetCode 208. 实现 Trie (前缀树)
class Trie {
private:
bool isEnd; //是否有以当前字母结尾的单词
Trie* next[26]; // 该字母后续的字节都有哪些
public:
Trie() {
isEnd = false;
memset(next, 0, sizeof(next));
}
void insert(string word) { //建树
Trie* node = this;
for (char c : word) {
if (node->next[c-'a'] == NULL) { //如果下一个节点为空,树种不存在该单词,则新建节点
node->next[c-'a'] = new Trie();
}
node = node->next[c-'a']; // 指针移动到下一个节点,直到把单词都插入
}
node->isEnd = true;
}
bool search(string word) {
Trie* node = this;
for (char c : word) {
node = node->next[c - 'a'];
if (node == NULL) {
return false;
}
}
return node->isEnd;
}
bool startsWith(string prefix) {
Trie* node = this;
for (char c : prefix) {
node = node->next[c-'a'];
if (node == NULL) {
return false;
}
}
return true;
}
};
作者:huwt
链接:https://leetcode.cn/problems/implement-trie-prefix-tree/solution/trie-tree-de-shi-xian-gua-he-chu-xue-zhe-by-huwt/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
LeetCode 211. 添加与搜索单词 - 数据结构设计
struct TrieNode{
vector<TrieNode *> child;
bool isEnd;
TrieNode() {
this->child = vector<TrieNode *>(26,nullptr);
this->isEnd = false;
}
};
void insert(TrieNode * root, const string & word) {
TrieNode * node = root;
for (auto c : word) {
if (node->child[c - 'a'] == nullptr) {
node->child[c - 'a'] = new TrieNode();
}
node = node->child[c - 'a'];
}
node->isEnd = true;
}
class WordDictionary {
public:
WordDictionary() {
trie = new TrieNode();
}
void addWord(string word) {
insert(trie,word);
}
bool search(string word) {
return dfs(word, 0, trie);
}
bool dfs(const string & word,int index,TrieNode * node) {
if (index == word.size()) {
return node->isEnd;
}
char ch = word[index];
if (ch >= 'a' && ch <= 'z') {
TrieNode * child = node->child[ch - 'a'];
if (child != nullptr && dfs(word, index + 1, child)) {
return true;
}
} else if (ch == '.') {
for (int i = 0; i < 26; i++) {
TrieNode * child = node->child[i];
if (child != nullptr && dfs(word, index + 1, child)) {
return true;
}
}
}
return false;
}
private:
TrieNode * trie;
};
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/design-add-and-search-words-data-structure/solution/tian-jia-yu-sou-suo-dan-ci-shu-ju-jie-go-n4ud/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。