软件工程专业必须要会的算法
Posted yjchen
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了软件工程专业必须要会的算法相关的知识,希望对你有一定的参考价值。
KMP算法
内容
??计算《部分匹配表》,移动位数 = 已匹配的字符数 - 对应的部分匹配值。
摘自
??点击查看原博主
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
void ComputePrefix(string s,int next[]){
int n = s.length();
int q,k;
next[0] = 0;
for(k=0,q=1;q<n;q++){
while(k>0 && 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;i<n;i++) {
while(q>0 && pattern[q]!=text[i])
q = next[q];
if(pattern[q]==text[i])
q++;
if(q==m)
{
cout<<"Pattern occurs with shift "<<i-m+1<<endl;
q=0;
}
}
}
int main()
{
string s = "abcdabcdebcd";
string p ="bcd";
KMPMatcher(s, p);
cout<<endl;
return 0;
}
Tries | 字典树
内容
??字典树(Trie)是一种很特别的树状信息检索数据结构,如同其名,它的构成就像一本字典,可以让你快速的进行字符插入、字符串搜索等。Trie 一词来自 retrieval,发音为 /tri:/ "tree",也有人读为 /tra?/ "try"。字典树设计的核心思想是空间换时间,所以数据结构本身比较消耗空间。但它利用了字符串的共同前缀(Common Prefix)作为存储依据,以此来节省存储空间,并加速搜索时间。Trie 的字符串搜索时间复杂度为 O(m),m 为最长的字符串的长度,其查询性能与集合中的字符串的数量无关。其在搜索字符串时表现出的高效,使得特别适用于构建文本搜索和词频统计等应用。
摘自
??点击查看原博主
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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
", "the", output[search(&trie, "the")]);
printf("%s --- %s
", "these", output[search(&trie, "these")]);
printf("%s --- %s
", "their", output[search(&trie, "their")]);
printf("%s --- %s
", "thaw", output[search(&trie, "thaw")]);
return 0;
}
以上是关于软件工程专业必须要会的算法的主要内容,如果未能解决你的问题,请参考以下文章
数据结构和算法必知必会的 50 道题目 12 种语言代码实现