KMP算法
内容
计算《部分匹配表》,移动位数 = 已匹配的字符数 - 对应的部分匹配值。
摘自
点击查看原博主
#include
#include
#include
using namespace std;
void ComputePrefix(string s,int next[]){
int n = s.length();
int q,k;
next[0] = 0;
for(k=0,q=1;q0 && s[k]!=s[q])
k = next[k];
if(s[k]==s[q])
k++;
next[q] = k;
}
}
void KMPMatcher(string text,string pattern) {
int n = text.length();
int m = pattern.length();
int next[pattern.length()];
ComputePrefix(pattern, next);
for(int i=0,q=0;i0 && pattern[q]!=text[i])
q = next[q];
if(pattern[q]==text[i])
q++;
if(q==m)
{
cout<<"Pattern occurs with shift "<
Tries | 字典树
内容
字典树(Trie)是一种很特别的树状信息检索数据结构,如同其名,它的构成就像一本字典,可以让你快速的进行字符插入、字符串搜索等。Trie 一词来自 retrieval,发音为 /tri:/ "tree",也有人读为 /traɪ/ "try"。字典树设计的核心思想是空间换时间,所以数据结构本身比较消耗空间。但它利用了字符串的共同前缀(Common Prefix)作为存储依据,以此来节省存储空间,并加速搜索时间。Trie 的字符串搜索时间复杂度为 O(m),m 为最长的字符串的长度,其查询性能与集合中的字符串的数量无关。其在搜索字符串时表现出的高效,使得特别适用于构建文本搜索和词频统计等应用。
摘自
点击查看原博主
#include
#include
#include
#define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])
// Alphabet size (# of symbols)
#define ALPHABET_SIZE (26)
// Converts key current character into index
// use only 'a' through 'z' and lower case
#define CHAR_TO_INDEX(c) ((int)c - (int)'a')
// trie node
typedef struct trie_node trie_node_t;
struct trie_node
{
int value;
trie_node_t *children[ALPHABET_SIZE];
};
// trie ADT
typedef struct trie trie_t;
struct trie
{
trie_node_t *root;
int count;
};
// Returns new trie node (initialized to NULLs)
trie_node_t *getNode(void)
{
trie_node_t *pNode = NULL;
pNode = (trie_node_t *)malloc(sizeof(trie_node_t));
if (pNode)
{
int i;
pNode->value = 0;
for (i = 0; i < ALPHABET_SIZE; i++)
{
pNode->children[i] = NULL;
}
}
return pNode;
}
// Initializes trie (root is dummy node)
void initialize(trie_t *pTrie)
{
pTrie->root = getNode();
pTrie->count = 0;
}
// If not present, inserts key into trie
// If the key is prefix of trie node, just marks leaf node
void insert(trie_t *pTrie, char key[])
{
int level;
int length = strlen(key);
int index;
trie_node_t *pCrawl;
pTrie->count++;
pCrawl = pTrie->root;
for (level = 0; level < length; level++)
{
index = CHAR_TO_INDEX(key[level]);
if (!pCrawl->children[index])
{
pCrawl->children[index] = getNode();
}
pCrawl = pCrawl->children[index];
}
// mark last node as leaf
pCrawl->value = pTrie->count;
}
// Returns non zero, if key presents in trie
int search(trie_t *pTrie, char key[])
{
int level;
int length = strlen(key);
int index;
trie_node_t *pCrawl;
pCrawl = pTrie->root;
for (level = 0; level < length; level++)
{
index = CHAR_TO_INDEX(key[level]);
if (!pCrawl->children[index])
{
return 0;
}
pCrawl = pCrawl->children[index];
}
return (0 != pCrawl && pCrawl->value);
}
// Driver
int main()
{
// Input keys (use only 'a' through 'z' and lower case)
char keys[][8] = { "the", "a", "there", "answer", "any", "by", "bye", "their" };
char output[][32] = { "Not present in trie", "Present in trie" };
trie_t trie;
initialize(&trie);
// Construct trie
for (int i = 0; i < ARRAY_SIZE(keys); i++)
{
insert(&trie, keys[i]);
}
// Search for different keys
printf("%s --- %s\n", "the", output[search(&trie, "the")]);
printf("%s --- %s\n", "these", output[search(&trie, "these")]);
printf("%s --- %s\n", "their", output[search(&trie, "their")]);
printf("%s --- %s\n", "thaw", output[search(&trie, "thaw")]);
return 0;
}
贪婪算法
内容
贪心算法(英語:greedy algorithm),又称贪婪算法,是一种在每一步选择中都采取在当前状态下最好或最优(即最有利)的选择,从而希望导致结果是最好或最优的算法。
摘自
点击查看原博