初始化

数组初始化在class外的话 要memset 在主函数里面memset

在class内不用

062. 实现前缀树

class Trie {
public:
/** Initialize your data structure here. */ int son[100010][26],cnt[100010],idx=0;
Trie() {
memset(son,0,sizeof son);
} /** Inserts a word into the trie. */
void insert(string word) {
int p=0;
for( char c: word)
{
int u=c-'a';
if(!son[p][u])son[p][u]=++idx;
p=son[p][u];
}
cnt[p]++;
} /** Returns if the word is in the trie. */
bool search(string word) {
int p=0;
for(char c:word){
int u=c-'a';
if(!son[p][u])return false;
p=son[p][u];
}
return cnt[p]>0; } /** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
int p=0;
for(char c:prefix){
int u=c-'a';
if(!son[p][u])return false;
p=son[p][u];
}
return true;
}
};

063. 替换单词

class Solution {
public:
int son[100010][26],cnt[100010],idx=0;
void insert(string s)
{
int p=0;
for(char c:s)
{
int u=c-'a';
if(!son[p][u])son[p][u]=++idx;
p=son[p][u];
}
cnt[p]++;
}
string check(string s)
{
string res;
int p=0;
for(char c:s)
{
int u=c-'a';
if(!son[p][u])break;
res+=c;
p=son[p][u];
if(cnt[p])return res;
}
if(cnt[p])return res;
return ""; }
string replaceWords(vector<string>& dictionary, string sentence) {
for(auto s:dictionary)insert(s);
stringstream ssin(sentence);
string ans,word;
while(ssin >>word)
{
//cout<<word<<"..."<<endl;
string x=check(word);
// cout<<x<<endl; if(x!="")
{
ans+=x;
}
else ans+=word;
ans+=" ";
}
return ans.substr(0,ans.size()-1); }
};

064. 神奇的字典

  const int N=10001;
int son[N][26],cnt[N],idx=0;
class MagicDictionary {
public:
/** Initialize your data structure here. */ MagicDictionary() {
memset(son,0,sizeof son);
memset(cnt,0,sizeof cnt);
idx=0; }
void insert(string s)
{
int p=0;
for(char c:s)
{
int u=c-'a';
if(!son[p][u])son[p][u]=++idx;
p=son[p][u];
}
cnt[p]++;
} void buildDict(vector<string> dictionary) {
for(auto s:dictionary)insert(s); }
bool dfs(string s,int p,int u,int op)
{
if(cnt[p]&&u==s.size()&&op==1)return true;//必须替换一个
if(op>1||s.size()==u)return false; //枚举改的字母
for(int i=0;i<26;i++)
{
if(!son[p][i])continue; if(dfs(s,son[p][i],u+1,op+(s[u]-'a'!=i)))return true;
}
return false; }
bool search(string searchWord) {
return dfs(searchWord,0,0,0);
}
};

065. 最短的单词编码

const int N=2000*7+10;

int son[N][26],cnt[N],len[N],idx=0;

class Solution {
public:
/*
这题是求叶节点长度
*/ void insert(string s)
{
reverse(s.begin(),s.end());
int p=0;
for(char c:s)
{
int u=c-'a';
if(!son[p][u])son[p][u]=++idx;
cnt[p]++;
p=son[p][u];
}
len[p]=s.size();
}
int minimumLengthEncoding(vector<string>& words) {
memset(son,0,sizeof son);
//cnt 我有多少个叶节点 0的话我就是
memset(cnt,0,sizeof cnt);//因为len依赖cnt 所以不用初始化
idx=0;
for(auto x:words)insert(x);
int res=0;
for(int i=1;i<=idx;i++)
if(!cnt[i])res+=len[i]+1;
return res; }
};

066. 单词之和

找到p再深搜

const int N=2501;
int son[N][26],cnt[N],idx=0;
class MapSum {
public:
/** Initialize your data structure here. */
MapSum() {
memset(son,0,sizeof son);
memset(cnt,0,sizeof cnt);
idx=0; } void insert(string key, int val) {
int p=0;
for(char c:key)
{
int u=c-'a';
if(!son[p][u])son[p][u]=++idx;
p=son[p][u];
}
cnt[p]=val;
}
int getp(string s)
{
int p=0;
for(char c:s)
{
int u=c-'a';
if(!son[p][u])return 0;
p=son[p][u];
}
return p;
}
int dfs(int p)
{
int sum=0;
if(cnt[p])sum+=cnt[p];
for(int i=0;i<26;i++)
{
if(son[p][i])sum+=dfs(son[p][i]);
}
return sum;
} int sum(string prefix) {
int p=getp(prefix);
if(p==0)return 0;
return dfs(p); }
};

067. 最大的异或

class Solution {
public:
int son[800001][2],idx=0;
void insert(int x)
{
int p=0;
for(int i=30;i>=0;i--)
{
int u=x>>i&1;
if(!son[p][u])son[p][u]=++idx;
p=son[p][u];
}
}
int query(int x)
{
int p=0,res=0;
for(int i=30;i>=0;i--)
{
int u=x>>i&1;
if(son[p][!u])
{
res=res*2+1;
p=son[p][!u];
}
else
{
res=res*2+0;
p=son[p][u];
}
}
return res;
}
int findMaximumXOR(vector<int>& nums) {
for(int x:nums)insert(x);
int res=0;
for(int x:nums)res=max(res,query(x));
return res; }
};

剑指 Offer II Trie前缀树的更多相关文章

  1. 刷题-力扣-剑指 Offer II 055. 二叉搜索树迭代器

    剑指 Offer II 055. 二叉搜索树迭代器 题目链接 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/kTOapQ 著作权归领扣网络所有 ...

  2. 剑指Offer - 九度1520 - 树的子结构

    剑指Offer - 九度1520 - 树的子结构2013-11-30 22:17 题目描述: 输入两颗二叉树A,B,判断B是不是A的子结构. 输入: 输入可能包含多个测试样例,输入以EOF结束.对于每 ...

  3. 【剑指 Offer II 001. 整数除法】同leedcode 29.两数相除

    剑指 Offer II 001. 整数除法 解题思路 在计算的时候将负数转化为正数,对于32位整数而言,最小的正数是-2^31, 将其转化为正数是2^31,导致溢出.因此将正数转化为负数不会导致溢出. ...

  4. 【剑指offer】q50:树节点最近的祖先

    #@ root: the root of searched tree #@ nodeToFind: the tree-node to be found #@ path: the path from r ...

  5. 剑指offer(17)层次遍历树

    题目: 从上往下打印出二叉树的每个节点,同层节点从左至右打印. public class Solution { ArrayList<Integer> list = new ArrayLis ...

  6. 剑指offer(17)树的子结构

    题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) 题目分析 分析如何判断树B是不是树A的子结构,只需要两步.很容易看出来这是一个递归的过程.一般在树 ...

  7. [剑指Offer]判断一棵树为平衡二叉树(递归)

    题目链接 https://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId=0&tqId=0&rp=2&a ...

  8. 【剑指Offer】17、树的子结构

      题目描述:   输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构)   解题思路:   要查找树A中是否存在和树B结构一样的子树,我们可以分为两步:第一步, ...

  9. 剑指Offer:面试题18——树的子结构(java实现)

    问题描述: 输入两棵二叉树A和B,判断B是不是A的子结构.二叉树结点的定义如下: public class TreeNode { int val = 0; TreeNode left = null; ...

  10. 【剑指offer】Q18:树的子结构

    类似于字符串的匹配,我们总是找到第一个匹配的字符,在继续比較以后的字符是否所有同样,假设匹配串的第一个字符与模式串的第一个不同样,我们就去查看匹配串的下一个字符是否与模式串的第一个同样,相应到这里,就 ...

随机推荐

  1. 强大的Excel工具,简便Vlookup函数操作:通用Excel数据匹配助手V2.0

    通用Excel数据匹配助手V2.0 For Windows 通用Excel数据匹配助手是一款非常实用的数据匹配软件,可以用来代替Excel中的Vlookup函数,帮助用户轻松完成数据匹配操作,需要的朋 ...

  2. linux下删除文件夹的软链接时注意千万不能在后面加反斜杠,千万不要用强制删除,否则下面2种场景,你会把源文件删除,要闯祸的

    今天遇到一个坑,自己在子目录下创建了父目录的软链接,导致可以无限循环进入父目录 [clouder@ana53 dir1]$ ll total 8 -rw-rw-r-- 1 clouder cloude ...

  3. 【模板】AC自动机(二次加强版)

    模板 \(Problem:\) 求 \(n\) 个模式串在文本串中出现的次数 \(templete:\) \(Luogu5357\) \(Code\) #include<cstdio> # ...

  4. 教你快速做一个自己的“ChatGPT”

    摘要:在国内使用ChatGPT有些不便,是否可以基于OpenAI开放的API做一个给自己或者同事们使用的聊天机器人,甚至集成到更多的场景-. 本文分享自华为云社区<使用 FunctionGrap ...

  5. Where do I Turn?(叉积)

    题目: Trouble came from the overseas lands: a three-headed dragon Gorynych arrived. The dragon settled ...

  6. 探索 C 语言的指针

    指针的概念 指针代表一个变量的内存地址,通过&可以拿到变量的内存地址.变量不等于指针,通过*可以拿到指针所指向的变量的值. 在 C 中,存在指针变量,指针变量的声明格式:int* varNam ...

  7. LeetCode-1220 统计元音字母序列的数目

    来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/count-vowels-permutation 题目描述 给你一个整数 n,请你帮忙统计一下我们 ...

  8. jquery获得标签的值或元素的内容

    例如: .html() 获取a标签中的i元素 console.error($("a[name=" + index + "]").html()); 设置a标签里的 ...

  9. java学习日记20230228-数据类型及加号运算

    程序中+使用: 1.两侧是数值型,则相加: 2.一方为字符串,则拼接: 3.运算顺序从做到右: 数据类型 每一种数据都定义了明确的数据类型,在内存中分配了不同大小的内存空间: java数据类型 基本数 ...

  10. 2023 年 CCF 春季测试赛模拟赛 - 1

    T1 个人思路: 询问:求 \(1\) 到 \(t_i\) 路径上离 \(1\) 最远的 \(p\),使得 \(dis_{1,p} \times 2 \le d_i\).即 \(dis_{1,t} \ ...