- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试实现 TopCoder 中所示的 trie页。我正在对其进行一些修改以存储用户的电话号码。我遇到段错误。有人可以指出错误吗。
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node{
int words;
int prefix;
long phone;
struct node* children[26];
};
struct node* initialize(struct node* root) {
root = new (struct node);
for(int i=0;i<26;i++){
root->children[i] = NULL;
}
root->word = 0;
root->prefix = 0;
return root;
}
int getIndex(char l) {
if(l>='A' && l<='Z'){
return l-'A';
}else if(l>='a' && l<='z'){
return l-'a';
}
}
void add(struct node* root, char * name, int data) {
if(*(name)== '\0') {
root->words = root->words+1;
root->phone = data;
} else {
root->prefix = root->prefix + 1;
char ch = *name;
int index = getIndex(ch);
if(root->children[ch]==NULL) {
struct node* temp = NULL;
root->children[ch] = initialize(temp);
}
add(root->children[ch],name++, data);
}
}
int main(){
struct node* root = NULL;
root = initialize(root);
add(root,(char *)"test",1111111111);
add(root,(char *)"teser",2222222222);
cout<<root->prefix<<endl;
return 0;
}
在进行建议更改后添加了一个新函数:
void getPhone(struct node* root, char* name){
while(*(name) != '\0' || root!=NULL) {
char ch = *name;
int index = getIndex(ch);
root = root->children[ch];
++name;
}
if(*(name) == '\0'){
cout<<root->phone<<endl;
}
}
最佳答案
改变这个:
add(root->children[ch], name++, data);
// ---------------------^^^^^^
对此:
add(root->children[ch], ++name, data);
// ---------------------^^^^^^
我将此代码中的其余问题留给您,但这是您运行调用堆栈的原因。
编辑 OP 要求进一步分析,虽然我通常不这样做,但这是一个相当简单的应用程序,可以扩展。
这是在几个地方完成的:
int index = getIndex(ch);
root = root->children[ch];
... etc. continue using ch instead of index
它回避了一个问题:“为什么我们只是要求一个我们立即忽略并且无论如何都使用 char 的索引?”这是在 add 中完成的()
和 getPhone()
。在为 children[]
数组中的所有 peeks 计算之后,您应该使用 index
。
此外,initialize()
函数需要改进或彻底抛弃,以支持基于构造函数的解决方案,该代码真正属于该解决方案。最后,如果这个 trie 应该跟踪每个级别参与的生成的单词和前缀的使用计数,我不清楚为什么你需要 both 单词和前缀计数器,但在任何一种情况下都需要更新您在 add()
中递归体面的计数器应该在反向递归上增加它们。
关于c++ - C++ 中的 Trie 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16124056/
我正在尝试做的事情: 构建一个移动网络应用程序,用户可以在玩拼字游戏时获得帮助找到要玩的单词 用户通过输入任意数量的字母和 0 个或多个通配符来获得单词建议 我是如何尝试做到这一点的: 使用 MySQ
假设我有一个 trie 包含多个字符串的数据结构。要在 trie 中查找字符串,我会从根开始,然后按顺序跟随标有字符串适当字符的指针,直到到达给定节点。 现在假设我想为同一组字符串构建一个“反向 tr
我正在阅读 Ingersoll、Morton 和 Farris 撰写的 Taming Text,但我不明白 solr 的数字 trie 实现如何帮助搜索文本?我对 solr.TrieField fie
我正在阅读 Ingersoll、Morton 和 Farris 的 Taming Text,但我不明白 solr 的数字 trie 实现如何帮助搜索文本?我对 solr 的 solr.TrieFiel
我正在开发一个 Trie 数据结构,其中每个节点代表一个词。所以词 st, stack, stackoverflow 和 overflow 将被排列为 root --st ---stack -----
trie 和radix trie 数据结构是一回事吗? 如果它们不相同,那么 radix trie (AKA Patricia trie) 是什么意思? 最佳答案 基数树是 trie 的压缩版本。在
我用过 video理解前缀特里树(虽然最终我试图最终得到后缀特里树)但是示例代码的链接被破坏所以我从视频中想出了这个,有两个功能,即插入和搜索如下 void insert(string word)
本文关键词:Leetcode, 力扣,Trie, 前缀树,字典树,208,Python, C++, Java 题目地址:https://leetcode.com/problems/implement
这是我使用 trie.c 和 trie.h 制作的主文件。该程序的目的是存储字典文件中的单词。 node* x = (node*)malloc(sizeof(node)); x = insert("b
因此,我必须搜索缺少字母(用于填字游戏)的单词,并且还必须维护剩余空格的可能单词列表。 现在我的问题是,我已经用谷歌搜索了 burst-trie 是否是最快的搜索算法。但是,如果我在 trie 中编写
我正在尝试将这个 trie 实现用于 ocaml:http://www.lri.fr/~filliatr/ftp/ocaml/ds/trie.ml.html 这是我对模块“M”的实现: module
我试图将 trie 中的所有单词放入字符串中,单词由 eow 字段表示,对于 trie 数据结构中的某个字符为 true,因此 trie 可以有字母但没有单词,例如“abc”在 trie 中,但“c”
我不太了解尝试字符串匹配中使用的实际算法。 我想知道为什么似乎更关注字符串匹配的后缀尝试而不是前缀尝试。我们可以不使用前缀尝试来进行子字符串匹配吗?换句话说,后缀尝试相对于前缀尝试有什么优势? 最佳答
当你构建一个特里树时,你是否将字符串/句子存储在其分支的末尾,以便在分支的末尾轻松访问它?有些人这样做,我有时也这样做,但我应该这样做吗? 有时(尤其是使用 LeetCode),我会收到此错误: Li
鉴于以下情况... (def inTree '((1 2) (1 2 3) (1 2 4 5 9) (1 2 4 10 15) (1 2 4 20 25))) 你如何将它转换
我想为高棉语(一种单词之间没有空格的语言)添加一个开源 Java 单词分割程序。开发人员已经很长时间没有开发它了,我无法联系他们了解详细信息(http://sourceforge.net/projec
我有一个字典文件(仅包含小写字母和撇号的单词),它作为特里树加载。 我有一个检查函数,它检查文件中的单词是否存在于特里树中树,无论字母大小写。 一切正常,除了撇号的单词总是拼写错误。 这是我的函数 b
我一直在练习 trie 数据结构(与类(class)作业无关)。此类用于存储字符串的子字符串。对于长度为 n 的字符串,总共有 n(n+1)/2 个子字符串。特别是 trie 的这种实现保留了自然顺序
所以我创建了一个包含大量数据的 trie,我的搜索算法非常快,但我想看看是否有人知道我如何才能让它更快。 bool search (string word) { int wordLen
我正在尝试用 C++ 实现 Trie,但出现运行时错误... 这是我的代码: #include using namespace std; struct trie{ bool word = f
我是一名优秀的程序员,十分优秀!