#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
class TrieNode;
class TrieNode
{
public:
int pass;
int end;
TrieNode* nexts;
TrieNode()
{
pass=0;
end=0;
nexts=(TrieNode*)calloc(sizeof(TrieNode),26);
}
};
class Trie
{
public:
TrieNode root;
Trie()
{
root=*(new TrieNode);
}
void insert(string str)
{
if(str.size()==0)
{
return;
}
TrieNode node=root;
node.pass++;
int index=0;
for(int i = 0;i < str.size();i++)
{
index=str[i]-'a';
if(node.nexts[index]!=NULL)
{
}
}
}
};
int main(int argc, char** argv){
string a ="abc";
Trie t ;
t.insert(a);
return 0;
}
我想知到申请的TrieNode数组的第index个位置是否为空,为空就创建结点,但是现在这样比较会报错,求说一下
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- GISer Liu 2023-02-26 15:04关注
以下答案引用自GPT-3大模型,请合理使用:
: // 首先,你可以先在建立TrieNode时,将nexts改为vector<TrieNode*> // 然后,改写你的insert函数,代码可以参考如下: void insert(string str) { if (str.size() == 0) return; TrieNode node = root; node.pass++; int index = 0; for (int i = 0; i < str.size(); i++) { index = str[i] - 'a'; if (node.nexts.size() <= index || node.nexts[index] == NULL) { // 判断当前的位置是否为空,若为空则新建结点 node.nexts[index] = new TrieNode(); } node = node.nexts[index]; node.pass++; } node.end++; }
如果我的回答解决了您的问题,请采纳我的回答
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 无线电能传输系统MATLAB仿真问题
- ¥50 如何用脚本实现输入法的热键设置
- ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
- ¥30 深度学习,前后端连接
- ¥15 孟德尔随机化结果不一致
- ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
- ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
- ¥15 谁有desed数据集呀
- ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
- ¥15 关于#hadoop#的问题