Pattern Searching using a Trie of all Suffixes
来源:互联网 发布:java fastjson转map 编辑:程序博客网 时间:2024/05/17 06:57
以下是构造banana\0的后缀树:
具体代码如下:
#include <iostream>#include <list>#define MAX_CHAR 256using namespace std;class SuffixTrieNode{private:SuffixTrieNode *children[MAX_CHAR];list<int> *indexes;public:SuffixTrieNode(){indexes = new list<int>;for(int i = 0; i < MAX_CHAR; ++i)children[i] = NULL;}void insertSuffix(string suffix, int index);list<int>* search(string pat);};class SuffixTrie{private:SuffixTrieNode root;public:SuffixTrie(string txt){for(int i = 0; i < txt.length(); ++i)root.insertSuffix(txt.substr(i), i);};void search(string pat);};void SuffixTrieNode::insertSuffix(string suffix, int index){indexes->push_front(index);if(suffix.length() > 0){char cIndex = suffix.at(0);if(children[cIndex] == NULL)children[cIndex] = new SuffixTrieNode();children[cIndex]->insertSuffix(suffix.substr(1), index + 1);}}list<int>* SuffixTrieNode::search(string pat){if(pat.length() == 0)return indexes;if(children[pat.at(0)] != NULL)return children[pat.at(0)]->search(pat.substr(1));elsereturn NULL;} void SuffixTrie::search(string pat){list<int>* result = root.search(pat);if(result == NULL)cout<<"Pattern not found!"<<endl;else{int patLen = pat.length();for(list<int>::iterator it = result->begin(); it != result->end(); ++it)cout<<"Pattern found at position "<<*it - patLen<<endl;}}int main() {//freopen("input.txt","r",stdin); //freopen("output.txt", "w", stdout); string txt = "geeksforgeeks.org"; SuffixTrie S(txt); cout << "Search for 'ee'" << endl; S.search("ee"); cout << "\nSearch for 'geek'" << endl; S.search("geek"); cout << "\nSearch for 'quiz'" << endl; S.search("quiz"); cout << "\nSearch for 'forgeeks'" << endl; S.search("forgeeks");return 0;}
转载于:http://www.geeksforgeeks.org/pattern-searching-using-trie-suffixes/
0 0
- Pattern Searching using a Trie of all Suffixes
- Exclude all lines match a pattern using ls and grep
- Pattern Searching
- All of a Sudden
- Naive Pattern Searching
- [pattern searching][0]
- Find all prime factors of a number
- list all files of a pc
- All factors of a given number
- find all the permutation of a string
- print all permutation of a string(recursion)
- get all Palindromes of a string
- 1.20OpenCV-- a list of all modules
- Performance of Using Keys in SELECT with FOR ALL ENTRIES
- Print the bit pattern of a float
- zoj 3228 Searching the String(trie)
- Cascading all windows on a machine without using API's
- List all objects from a NamingService using JacORB
- 如何用BeginInvoke方法解决SetCurrentCellAddressCore 异常
- GitBlit中文空目录乱码+查看乱码解决
- 高考改革方案:文理不分科
- 字节数字和十六进制的转化
- 一段理解nio的代码
- Pattern Searching using a Trie of all Suffixes
- 慢慢的我开始懂了
- 权限
- 关于提示“Unknown parameter: --with-codecsdir=/usr/lib/codecs/”问题的解决
- 手机开发
- 盘点9个基于Java的搜索引擎框架特性
- 企业管理更新 - 最近的创新客户关系管理软件
- 利用NPOI导出数据到Execl
- ScrollView