这篇简单的谈谈后缀树原理及实现。

  如前缀树原理一般,后缀trie树是将字符串的每个后缀使用trie树的算法来构造。例如banana的所有后缀:

0: banana
1: anana
2: nana
3: ana
4: na
5: a

  按字典序排列后:

5: a
3: ana
1: anana
0: banana
4: na
2: nana

  形成一个树形结构。

  代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// banana中不重复的字符有:a b n
/*
* a b n
* n $ a a
* a n n $
* n $ a a
* a n $
* $ a
$*/
#define SIZE 27
#define Index(c) ((c) - 'a')
#define rep(i, a, b) for(i = a; i < b; i++)
typedef struct BaseNode {
struct BaseNode*next[SIZE];
char c;
int num;
} suffix_tree, *strie;
void initialize(strie* root)
{
int i;
*root = (strie)malloc(sizeof(suffix_tree));
(*root)->c = 0;
(*root)->num = -1;
rep(i, 0, SIZE) (*root)->next[i] = NULL;
}
void insert(strie*root, const char*str, int k)
{
suffix_tree*node = *root, *tail;
int i, j;
for (i = 0; str[i] != '\0'; i++)
{
if (node->next[Index(str[i])] == NULL)
{
tail = (strie)malloc(sizeof(suffix_tree));
tail->c = str[i];
tail->num = -1;
rep(j, 0, SIZE) tail->next[j] = NULL;
node->next[Index(str[i])] = tail;
}
node = node->next[Index(str[i])];
}
tail = (strie)malloc(sizeof(suffix_tree));
tail->c = '$';
tail->num = k;
rep(i, 0, SIZE) tail->next[i] = NULL;
node->next[SIZE - 1] = tail;
}
void show(suffix_tree*root)
{
if (root)
{
int i;
rep(i, 0, SIZE) show(root->next[i]);
printf("%c\n", root->c);
if (root->num > -1)
{
printf("%d\n", root->num);
}
}
}
void destory(strie*root)
{
if (*root)
{
int i;
rep(i, 0, SIZE) destory(&(*root)->next[i]);
free(*root);
*root = NULL;
}
}
int main()
{
suffix_tree*root; initialize(&root); char str[] = "banana", *p = str;
int i = 0;
while(*p)
{
insert(&root, p, i);
p++;
i++;
}
show(root);
destory(&root);
return 0;
}

  时间复杂度分析:算法中对于建立一串长m的字符串,需要一个外层的m次循环 + 一个内层m次循环 + 一些常数,于是建立一颗后缀字典树所需的时间为O(m2),27的循环在这里可看作常数;

  空间复杂度分析:一个字符的字符串长度为1,需要消耗的1个该字符 + 1个根节点 + 1个\$字符的空间,两个字符的字符串长度为2,需要消耗3个字符空间+ 1个根节点 + 2个\$空间...以此类推,发现总是含有1个根节点和m个\$字符,\$的个数等于字符串长度m,而存储的源字符串后缀所需的空间有如下规律:

$$ \begin{aligned} O(s_1) &= 1 \\ O(s_2) &= 1+2 \\ O(s_3) &= 1+2+3 \\ \cdot \cdot \cdot \\ O(s_m) &= 1+2+ \cdot \cdot \cdot + m \end{aligned} $$

  设以长为m的字符串s建立后缀树T,于是有:

$$ O(T) = O(\frac{(1 + m)m}{2} + 1 + m) = O(m^2) $$

  由于上面算法对于无重复的字符串来说空间复杂度比较大,所以使用路径压缩以节省空间,这样的树就称为后缀树,也可以通过下标来存储,如图:

  p.s.写压缩路径的后缀树时,脑子犯傻了...错了,改天再把正确的补上。。。

  路径压缩版后缀树:

#include <iostream>
using namespace std;
#define rep(i, a, b) for(int i = a; i < b; i++)
#define trans(c) (c - 'a')
#define SIZE 26
#define MAX (100010 << 2)
struct BaseNode {
int len;
const char*s;
int pos[MAX];
BaseNode*next[SIZE];
BaseNode()
{
len = 0;
rep(i, 0, MAX) pos[i] = 0;
rep(i, 0, SIZE) next[i] = nullptr;
}
BaseNode(const char*s, int p)
{
this->s = s, this->len = p;
rep(i, 0, MAX) pos[i] = 0;
rep(i, 0, SIZE) next[i] = nullptr;
}
};
class SuffixTree {
private:
BaseNode*root;
/**/
void add(const char*s, int p);
void print(BaseNode*r);
void destory(BaseNode*&r);
public:
SuffixTree()
{
root = nullptr;
}
void insert(const char*s);
void insert(string s)
{
insert(s.c_str());
}
void remove(const char*s)
{ }
void visual()
{
print(root);
}
bool match(const char*s);
bool match(string s)
{
match(s.c_str());
}
~SuffixTree()
{
destory(root);
}
};
void SuffixTree::add(const char*s, int p)
{
int i = 0; while (s[i]) i++;
if (!root->next[p]) root->next[p] = new BaseNode(s, i);
root->next[p]->pos[i] = i;
}
void SuffixTree::insert(const char*s)
{
root = new BaseNode();
while (*s)
{
add(s, trans(*s));
s++;
}
}
bool SuffixTree::match(const char*s)
{
const char* ps = root->next[trans(*s)]->s;
while (*s) if (*ps++ != *s++) return false;
return true;
}
void SuffixTree::print(BaseNode*r)
{
if (r)
{
rep(i, 0, SIZE)
if (r->next[i])
{
cout << i << ':' << endl;
rep(j, 0, r->next[i]->len + 1)
if (r->next[i]->pos[j])
{
rep(k, 0, r->next[i]->pos[j])
cout << r->next[i]->s[k];
cout << '$' << endl;
}
}
}
}
void SuffixTree::destory(BaseNode*&r)
{
if (r)
{
rep(i, 0, SIZE) destory(r->next[i]);
delete r;
}
}
int main()
{
SuffixTree st;
st.insert("banana");
st.visual();
if (st.match("na")) cout << "Yes" << endl;
else cout << "No" << endl;
return 0;
}

  上面的后缀树都是对于一个字符串的处理方法,而广义后缀树将算法推广到了不同的字符串上,但我还没写过,改天补上。。。

  参考:https://en.wikipedia.org/wiki/Suffix_tree

Suffix Tree(后缀树)的更多相关文章

  1. 后缀树(suffix tree)

    参考: 从前缀树谈到后缀树 后缀树 Suffix Tree-后缀树 字典树(trie树).后缀树 一.前缀树 简述:又名单词查找树,tries树,一种多路树形结构,常用来操作字符串(但不限于字符串), ...

  2. Trie / Radix Tree / Suffix Tree

    Trie (字典树) "A", "to", "tea", "ted", "ten", "i ...

  3. Trie树(代码),后缀树(代码)

    Trie树系列 Trie字典树 压缩的Trie 后缀树Suffix tree 后缀树--ukkonen算法 Trie是通过对字符串进行预先处理,达到加快搜索速度的算法.即把文本中的字符串转换为树结构, ...

  4. 后缀树(Suffix Tree)

          问题描述:               后缀树(Suffix Tree)   参考资料: http://www.cppblog.com/yuyang7/archive/2009/03/29 ...

  5. 后缀树的建立-Ukkonen算法

    参考: Ukkonen算法讲解 Ukkonen算法动画 Ukkonen算法,以字符串abcabxabcd为例,先介绍一下运算过程,最后讨论一些我自己的理解. 需要维护以下三个变量: 当前扫描位置# 三 ...

  6. 笔试算法题(40):后缀数组 & 后缀树(Suffix Array & Suffix Tree)

    议题:后缀数组(Suffix Array) 分析: 后缀树和后缀数组都是处理字符串的有效工具,前者较为常见,但后者更容易编程实现,空间耗用更少:后缀数组可用于解决最长公共子串问题,多模式匹配问题,最长 ...

  7. Suffix树,后缀树

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  8. 后缀树(Suffix Trie)子串匹配结构

    Suffix Trie 又称后缀Trie或后缀树.它与Trie树的最大不同在于,后缀Trie的字符串集合是由指定字符串的后缀子串构成的.比如.完整字符串"minimize"的后缀子 ...

  9. CF504E Misha and LCP on Tree 后缀自动机+树链剖分+倍增

    求树上两条路径的 LCP (树上每个节点代表一个字符) 总共写+调了6个多小时,终于过了~ 绝对是我写过的最复杂的数据结构了 我们对这棵树进行轻重链剖分,然后把所有的重链分正串,反串插入到广义后缀自动 ...

随机推荐

  1. 国内某Python大神自创完整版,系统性学习Python

    很多小伙伴纠结于这个一百天的时间,我觉得完全没有必要,也违背了我最初放这个大纲上来的初衷,我是觉得这个学习大纲还不错,自学按照这个来也能相对系统的学习知识,而不是零散细碎的知识最后无法整合,每个人的基 ...

  2. 普及C组第四题(8.2)

    1342. [南海2009初中]cowtract(网络) (Standard IO) 题目:  Bessie受雇来到John的农场帮他们建立internet网络.农场有 N (2<= N < ...

  3. linux搭建常用命令

    nohup java -jar floodlight.jar >log.txt     运行jar,日志打印到log.txt中netstat -lnp|grep 88            查看 ...

  4. 【C语言】输入一个字符串,统计其中的单词个数,将第一个单词的首字母改为大写,并输出改写后的字符串

    #include<stdio.h> int main() { ]; ; printf("请输入一串字符:"); gets_s(a); ; a[i] != '\0'; i ...

  5. springboot的安装与初步使用

    1.引用springboot框架 1.在maven项目底下的pom.xml的中,引用springboot,如下 <?xml version="1.0" encoding=&q ...

  6. Centos6.10-FastDFS-Storage-Nginx配置

    nginx+fastdfs-nginx-module 安装此处省略. 1.进入工作目录 cd /usr/local/nginx/conf 2.创建子目录 mkdir configs 3.创建stora ...

  7. zol验证码抓包

    http://www.zol.com/detail/lcd/samsung/25254662.html?zp_from=pro_price_pricenode 网站’ http://city.zol. ...

  8. AD10如何新建一个集成库

    1.新建一个集成库工程 2.添加原理图库 3.添加封装库 4.最关键的一步:对这个工程需要进行编译 5.在界面的右边就能看到自己建的封装库了

  9. C:作用域

    作用域 C语言变量的作用域分为: 代码块作用域(代码块是{}之间的一段代码) 函数作用域 文件作用域 局部变量 局部变量也叫auto自动变量(auto可写可不写),一般情况下代码块{}内部定义的变量都 ...

  10. 对DensePose: Dense Human Pose Estimation In The Wild的理解

    研究方法 通过完全卷积学习从图像像素到密集模板网格的映射.将此任务作为一个回归问题,并利用手动注释的面部标注来训练我们的网络.使用这样的标注,在三维对象模板和输入图像之间,建立密集的对应领域,然后作为 ...