18.8 suffix tree

给定一个字符串s 和 一个包含较短字符串的数组t,设计一个方法,根据t中的每一个较短的字符换,对s进行搜索。

思路:基于s建立后缀树,然后在后缀树中进行查找,返回所有可能的索引值。

import java.util.ArrayList;

public class Question {
    public static void main(String[] args) {
        String testString = "bibs";
        String[] stringList = {"b", "bs", "hi", "bb"};
        SuffixTree tree = new SuffixTree(testString);
        for (String s : stringList) {
            ArrayList<Integer> list = tree.search(s);
            if (list != null) {
                System.out.println(s + ": " + list.toString());
            }
        }
    }
}

/**

b: [0, 2]

bs: [2]

*/

 
import java.util.ArrayList;

public class SuffixTree {
    SuffixTreeNode root = new SuffixTreeNode();

    public SuffixTree(String s) {
        for (int i = 0; i < s.length(); i++) {
            String suffix = s.substring(i);
            root.insertString(suffix, i);
        }
    }

    public ArrayList<Integer> search(String s) {
        return root.search(s);
    }
}
package Question18_8;

import java.util.ArrayList;
import java.util.HashMap;

public class SuffixTreeNode {
    HashMap<Character, SuffixTreeNode> children = new HashMap<Character, SuffixTreeNode>();

    char value;
    ArrayList<Integer> indexes = new ArrayList<Integer>();
    public SuffixTreeNode() { }

    public void insertString(String s, int index) {
        indexes.add(index);
        if (s != null && s.length() > 0) {
            value = s.charAt(0);
            SuffixTreeNode child = null;
            if (children.containsKey(value)) {
                child = children.get(value);
            } else {
                child = new SuffixTreeNode();
                children.put(value, child);
            }
            String remainder = s.substring(1);
            child.insertString(remainder, index);
        }
    }

    public ArrayList<Integer> search(String s) {
        if (s == null || s.length() == 0) {
            return indexes;
        } else {
            char first = s.charAt(0);
            if (children.containsKey(first)) {
                String remainder = s.substring(1);
                return children.get(first).search(remainder);
            }
        }
        return null;
    }

}
时间: 2024-10-11 06:49:05

18.8 suffix tree的相关文章

[算法系列之二十四]后缀树(Suffix Tree)

之前有篇文章([算法系列之二十]字典树(Trie))我们详细的介绍了字典树.有了这些基础我们就能更好的理解后缀树了. 一 引言 模式匹配问题 给定一个文本text[0-n-1], 和一个模式串 pattern[0-m-1],写一个函数 search(char pattern[], char text[]), 打印出pattern在text中出现的所有位置(n > m). 这个问题已经有两个经典的算法:KMP算法 ,有限自动机,前者是对模式串pattern做预处理,后者是对待查证文本text做预处

编程算法 - 后缀树(Suffix Tree) 代码(C)

后缀树(Suffix Tree) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 给你一个长字符串s与很多短字符串集合{T1,, T2, ...}, 设计一个方法在s中查询T1, T2, ..., 要求找出Ti在s中的位置. 代码: /* * main.cpp * * Created on: 2014.7.20 * Author: Spike */ /*eclipse cdt, gcc 4.8.1*/ #include <iostream> #i

Trie / Radix Tree / Suffix Tree

Trie (字典树) "A", "to", "tea", "ted", "ten", "i", "in", "inn" 这些单词组成的字典树. Radix Tree (基数树) 基数树与字典树的区别在于基数树将单词压缩了, 节点变得更少 Suffix Tree (后缀树) 单词 "BANANA" 的后缀树. 每个后缀以 $ 结尾

Suffix Tree (from geeksforgeeks)

Given a text txt[0..n-1] and a pattern pat[0..m-1], write a function search(char pat[], char txt[]) that prints all occurrences of pat[] in txt[]. You may assume that n > m. Preprocess Pattern or Preoprocess Text? We have discussed the following algo

leetCode(18):Construct Binary Tree from Preorder and Inorder (Inorder and Postorder) Traversal

Given preorder and inorder (Inorder and Postorder) traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 前序和后序的特点是根结点要么在最前面.要么在最后面.已知根结点后,以根结点为界将中序遍历的结果分成两段,然后递归即可还原二叉树.有两点需要注意:首先二叉树中不能有重复的结点:其

SPOJ 375. Query on a tree (树链剖分)

Query on a tree Time Limit: 5000ms Memory Limit: 262144KB This problem will be judged on SPOJ. Original ID: QTREE64-bit integer IO format: %lld      Java class name: Main Prev Submit Status Statistics Discuss Next Font Size: + - Type:   None Graph Th

A1043. Is It a Binary Search Tree (25)

Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater

【转】HDU-6035-Colorful Tree

转自http://blog.csdn.net/Bahuia/article/details/76141574 题意: 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6035一棵n个结点的树,每个结点都有颜色,定义两点之间的路径长度为路径上出现的不同颜色数目,求树上所有路径的长度和. 思路: "真的难"系列. 首先这题肯定是算贡献,也就是计算出每种颜色参与了多少条路径,但这样正面考虑并不容易,不妨从反面考虑,计算每种颜色没有参与多少路径,然

1099. Build A Binary Search Tree (30)

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greate