- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
C 中的这段代码输入了一个整数和一个字符串,并应在输出中按字典顺序给出字符串的 3 个连续字母的所有字谜。
EXAMPLE:
Input
3
mississippi
Output
ipp
iss
iss
mis
ppi
sip
sis
ssi
ssi
它没有,而是只给出最后一个 3-gram 重复 n 次,其中 n=可能的 3-gram 的数量。
我使用字符串的二叉搜索树来记住每个字谜,我不能使用简单的字符串数组,因为练习需要这样做。为了按顺序打印字符串,它会进行对称访问(从左到右)。
我做了一些调试测试,我认为问题出在插入函数上,可能是我把标点符号弄得一团糟。
如果有人想看一下,我将不胜感激
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
typedef struct nodo
{
char * key;
int dim;
struct nodo * left;
struct nodo * right;
} Nodo;
typedef Nodo* albero;
/*int dimensions (albero t)
{
int d;
if (t==NULL) return 0;
else d=(1+dimensions(t->left)+dimensions(t->right));
t->dim=d;
return d;
}*/
simmetrica(albero t,int z)
{
if (t!=NULL)
{
simmetrica(t->left,z);
/*if ((t->dim)>=z)*/ printf("%s\n",t->key);
simmetrica(t->right,z);
}
}
Nodo* insert(albero t, char* key)
{
Nodo* new = malloc(sizeof(Nodo));
new->key=key;
new->dim=0;
new->right=NULL;
new->left=NULL;
if(t==NULL)
{
return new;
}
Nodo* parent;
Nodo* current=t;
while(current!=NULL)
{
parent=current;
if/*(current->key<key)*/(strcmp(current->key,key)<0)
{
current=current->right;
}
else
{
current=current->left;
}
}
if/*(parent->key<key)*/(strcmp(parent->key,key)<0)
{
parent->right=new;
}
else parent->left=new;
return t;
}
int main()
{
int z;
char s[501];
char trip[4];
albero t=malloc(sizeof(Nodo));
t=NULL;
scanf("%d", &z);
scanf("%s", s);
int o=0;
while (s[o+2] !='\0')
{ //printf("debug %d\n",o);
trip[0]=s[o];
trip[1]=s[o+1];
trip[2]=s[o+2];
trip[3]='\0';
//printf("trip %s\n",trip);
t=insert(t, trip);
o++;
}
//dimensions(t);
simmetrica(t,z);
return 0;
}
最佳答案
一切都与存储有关。 trip
已分配存储空间。它存在于某个地址,假设它是 0x401fc
。当您执行插入操作时,您将地址 0x401fc
传递到 insert
中。 Insert 创建一个类型为 struct nodo
的新记录,并将地址 0x401fc
复制到字段 key
中。
然后您继续更改 trip
中的值,这会更改 0x401fc
中的值,现在所有 struct nodo
都指向新值(value)观。
要么做一个 new-> key= strdup(key)
要么让 struct nodo
有一个 char key[4] ;
然后做一个 strcpy()
。任一解决方案都会创建新的存储空间。第一个在短期内是一个更简单的修复,但处理起来会更麻烦(因为它与结构分开存储)。第二种形式需要更多初始更改,但会更容易取消分配内存。
关于C - 在二叉搜索树中打印字符串的有序 3-Anagrams,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21314361/
过去两天我遇到了一个奇怪的问题,但我还无法解决它。我正在尝试从 2 个文本文件中获取单词并将这些单词添加到树中。我选择的获取单词的方法引用这里: Splitting a text file into
例如,像这样的树: 5 / \ 3 6 / \ 7 2 print(tree.branchLenSum()) 将是1+1+2+2=6 树类: class BinaryTre
我正在学习 JavaScript。我是文档对象模型 (DOM) 主题的新手。 我对节点在 DOM 树中的布局方式感到困惑? 我使用了这个 HTML 文件。 List Buy groceries
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
var nodeEnter = node.enter().append("g") .attr("class", "node") .attr("transform", function (d) {
我正在使用数据表来表示我的应用程序中的数据。我有一个功能,可以在单击克隆图标时克隆任何行并动态添加它下面的行。 数据表具有搜索功能,可以搜索表中存在的所有行,但是它只考虑初始页面加载时加载到表中的行。
我已经用 Python 编写了一个 Tree 类,但是我在为它创建迭代器时遇到了问题我希望能够做到 phonebook = MyTree() # Build up tree for node in p
我需要为 FreeBSD 检查这个 libunwind 端口, http://people.freebsd.org/~kib/git/libunwind.git/ 我以前和当我尝试使用命令 check
我已经实现了自己的 AVL 树,并将其用作字典。我想知道,计算以某个字符串开头的所有单词的最快方法是什么。 例如: string prefix = "fa"; output: 4 我已经在 O(n)
我目前正在尝试编写一个使用 2-3-4 树的程序,但我遇到了插入函数的问题。这是相关代码.. int main () { tree234 myTree; myTree.insert("
我的同事问了我这个问题,但我无法想出任何最佳解决方案。 给定一棵具有n 个节点、n-1 条边和 q 个查询的无向加权树。 每个查询都有输入 u v k ,输出位于路径 u to v 的奇数且小于 k
如果我有一个 app.html 模板如下: ${message} test 使用 MyComponent.ts : export class MyComponent { my
我继承了一个 git 存储库,其中树中的提交条目为空 sha1,阻止 FishEye 为存储库编制索引。 $ git fsck Checking object directoriies: 100%(2
这是一个例子。我也许可以在点击函数中弄清楚它,但我不确定从点击函数外部从根开始检查所有子项的最佳方法。谢谢 .node circle { fill: #fff; stroke: stee
我正在使用 Python 批量编辑许多当前看起来像这样的 musicXML 文件: ... -5 -9
假设有一个 8 阶 B 树。这意味着它可以有 8 个指针和 7 个元素。假设字母 A 到 G 存储在这棵 B 树中。所以这棵 B 树只是一个包含 7 个元素的节点。 然后你尝试将 J 插入到树中。没有
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我正在尝试做一个简单的二维“物理”模拟,主要涉及圆形物体的碰撞,为了避免编程我自己的空间索引(四叉树/r-tree/等),我希望使用 Boost 的 R -树。 问题是我在 Boost 文档中找不到任
我正在从 python 字典中读取数据并尝试在下面的树中添加更多书籍元素。下面只是一个示例,我需要复制一个元素及其子元素但替换内容,在这种情况下我需要复制 book 元素但替换标题和作者。
假设我有一个变量,它包含一个 nltk 树类的树。是否有类似 parent() 或返回节点父节点的函数? 最佳答案 您需要一种不同的数据结构:一棵树,其节点包含指向其父节点的指针。 NLTK 现在提供
我是一名优秀的程序员,十分优秀!