尝试和后缀树实现
回答
的Ç算法库(http://fragglet.github.io/c-algorithms/)
实现提供了一个Trie implementation in C。它是开源的,具有BSD风格的许可证。
C中的一个后缀树的实现可以在这里找到:https://github.com/0xtonyxia/suffix-tree
我希望帮助。
没有任何链接正在工作 – 2016-07-10 02:09:56
@Rasmi Ranjan Nayak:链接现在已修复 – 2016-07-14 18:54:13
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
typedef struct trie trie;
struct trie
{
char key;
trie *next,*children;
};
trie *newnode(char s)
{
trie *t=(trie *)malloc(sizeof(trie));
t->key=s;
t->next=t->children=NULL;
}
void insert(trie **t,char *s,int start)
{if(s[start]=='\0')
{
*t=newnode('#');
return;
}
if(*t==NULL)
{
*t=newnode(s[start]);
insert(&(*t)->children,s,start+1);
}
if((*t)->key==s[start])
insert(&(*t)->children,s,start+1);
else
insert(&(*t)->next,s,start);
}
bool search(trie *t ,char *s,int start)
{
if(t==NULL)
return false;
if(t->key=='#' && s[start]=='\0')
return true;
if(t->key!='#' && s[start]=='\0' || t->key=='#' && s[start]!='\0')
return false;
if(t->key==s[start])
return search(t->children,s,start+1);
else
return search(t->next,s,start);
return false;
}
/*void push(trie **t ,char *str)
{ int i=0;
for(i=0;i<strlen(str);i++)
insert(t,str[i]);
}*/
main()
{ int i=0;
trie *t=NULL;
char ch='y';
while(ch=='y')
{
{char str[20];
fflush(stdin);
printf("Enter the word ");
gets(str);
insert(&t,str,0);
}
// push(&t,str);
fflush(stdin);
printf("more y/n ::");
ch=getchar();
}
ch='y';
while(ch=='y')
{char str[20];
fflush(stdin);
printf("Enter the string you want to search::");
gets(str);
fflush(stdin);
if(search(t,str,0))
printf("Found");
else
printf("Not Found");
printf("\n more y/n ::");
scanf("%c",&ch);
}
getch();
}
下面是我发现的非常有用的链接。
上后缀树(讲座3至第5讲) 谷歌SCICOMP讲座56小时讲座(最长公共子串:O(n)的后缀树,排序后缀)
广义后缀树实现 http://www.geeksforgeeks.org/generalized-suffix-tree-1/
快速串与后缀树 http://marknelson.us/1996/08/01/suffix-trees/
搜索查找维基百科Ukkonen算法。 注意:无法发布超过2个链接会导致声誉不够。
#include <iostream>
#include <string.h>
using namespace std;
int high;
struct stnode
{
stnode* ptr[27];
int start;
int end;
stnode(int s,int e)
{
for (int i = 0; i < 27; ++i)
{
ptr[i]=NULL;
}
start=s;
end=e;
}
}*root=NULL;
stnode* fun(stnode* child,string str,int ind)
{
int s=child->start;
while(s<=child->end&&str.at(s)==str.at(ind))
{
s++;
ind++;
}
if(s<=child->end)
{
child->ptr[str.at(ind)-'a']=new stnode(ind,high);
if(str.at(s)=='$')
child->ptr[26]=new stnode(s,child->end);
else
child->ptr[str.at(s)-'a']=new stnode(s,child->end);
child->end=s-1;
return child;
}
else
{
if(child->ptr[str.at(ind)-'a'])
{
child->ptr[str.at(ind)-'a']=fun(child->ptr[str.at(ind)-'a'],str,ind);
return child;
}
else
{
child->ptr[str.at(ind)-'a']=new stnode(ind,high);
return child;
}
}
}
stnode* create(stnode* root,string str,int ind)
{
if(!root)
{
root=new stnode(ind,high);
return root;
}
if(str.at(ind)=='$')
{
root->ptr[26]=new stnode(ind,high);
return root;
}
if(!root->ptr[str.at(ind)-'a'])
{
root->ptr[str.at(ind)-'a']=new stnode(ind,high);
return root;
}
root->ptr[str.at(ind)-'a']=fun(root->ptr[str.at(ind)-'a'],str,ind);
return root;
}
void display(stnode* root,string str)
{
if(!root)
return;
if(root->start!=-1)
{
for(int i=root->start;i<=root->end;i++)
{
cout<<str.at(i);
}
cout<<"\n";
}
for(int i=0;i<27;i++)
{
display(root->ptr[i],str);
}
}
int main(int argc, char const *argv[])
{
string str;
cout<<"enter the string.\n";
cin>>str;
str=str+"$";
high=str.length()-1;
root=new stnode(-1,-1);
for(int i=str.length()-1;i>=0;i--)
{
root=create(root,str,i);
display(root,str);
cout<<"\n\n\n";
}
return 0;
}
您能提供一些上下文吗? – Robert 2015-03-16 10:20:50
- 1. 在C#中寻找后缀树实现?
- 2. 简短的Java后缀树的实现和用法?
- 3. 基本前缀树实现问题
- 4. PHPUnit的尝试给予空后缀
- 5. 天真的后缀树在Java中的实现
- 6. 后缀树:最长的重复子字符串实现
- 7. 大数据集的广义后缀树Java实现
- 8. 什么被认为是最好的Java后缀树实现?
- 9. 从后缀树生成后缀
- 10. 后缀树构造
- 11. Python:假实际尝试之前尝试和发现异常
- 12. 尝试(和失败)实现ActionBarSherlock
- 13. 尝试使用树枝从数据库呈现实体地址
- 14. 填充广义后缀树和实施资源
- 15. Matlab中的后缀树
- 16. 后缀树是否唯一?
- 17. JavaScript中的后缀树?
- 18. 令牌后缀树教程
- 19. 后缀树搜索时间
- 20. 在C++构建后缀树
- 21. 实现C++后缀增量操作
- 22. 在Java中后缀数组实现
- 23. 后缀数组实现错误
- 24. 使用OOP/C++实现后缀Trie
- 25. 后缀数组优于后缀树的位置?
- 26. 使用JUnit测试树实现
- 27. 尝试用树莓派
- 28. kd树实现
- 29. 尝试使用Google日历实现2.2.2
- 30. 尝试使用来实现例如HttpExchange
可能重复的[试图数据结构实现.........应用程序 - 字典............](http://stackoverflow.com/questions/ 3323117/tries-data-structure-implementation-application-dictionary) – 2010-07-27 03:25:02
如何通过选择答案来解决这个问题?既然你问了同一个问题,这个问题肯定是多余的。 – AnyOneElse 2013-09-04 13:01:05
Trie和Suffix树上的wikipedia文章为Trie提供了很好的解释和伪代码 – stan0 2015-03-16 13:53:24