码迷,mamicode.com
首页 > 编程语言 > 详细

软件工程专业必须要会的算法

时间:2020-02-04 16:02:20      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:node   match   ptr   std   http   href   next   查询   移动   

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\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;
}

软件工程专业必须要会的算法

标签:node   match   ptr   std   http   href   next   查询   移动   

原文地址:https://www.cnblogs.com/yjchen/p/12259492.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!