【问题标题】:Using array instead of set<string>使用数组代替 set<string>
【发布时间】:2020-04-04 19:02:08
【问题描述】:

这是一个用于查找 X 和 Y 序列的所有最长公共序列的功能。 但是这个程序是用 c++ 编写的,但我想用 C 编写它。

有没有办法用数组代替集合?

例如。如果输入是

X = &lt; A, A, T, C, C, &gt;

Y = &lt; A, C, A, C, G, &gt;

那么输出应该是

&lt; A, C, C, &gt;

&lt; A, A, C, &gt;

m 和 n 分别是序列 X 和 Y 的大小。

/* source : https://www.geeksforgeeks.org/printing-longest-common-subsequence-set-2-printing/ */

/* Returns set containing all LCS for X[0..m-1], Y[0..n-1] */
set<string> findLCS(string X, string Y, int m, int n) 
{ 
    // construct a set to store possible LCS 
    set<string> s; 

    // If we reaches end of either string, return 
    // a empty set 
    if (m == 0 || n == 0) 
    { 
        s.insert(""); 
        return s; 
    } 

    // If the last characters of X and Y are same 
    if (X[m - 1] == Y[n - 1]) 
    { 
        // recurse for X[0..m-2] and Y[0..n-2] in 
        // the matrix 
        set<string> tmp = findLCS(X, Y, m - 1, n - 1); 

        // append current character to all possible LCS 
        // of substring X[0..m-2] and Y[0..n-2]. 
        for (string str : tmp) 
            s.insert(str + X[m - 1]); 
    } 

    // If the last characters of X and Y are not same 
    else
    { 
        // If LCS can be constructed from top side of 
        // the matrix, recurse for X[0..m-2] and Y[0..n-1] 
        if (L[m - 1][n] >= L[m][n - 1]) 
            s = findLCS(X, Y, m - 1, n); 

        // If LCS can be constructed from left side of 
        // the matrix, recurse for X[0..m-1] and Y[0..n-2] 
        if (L[m][n - 1] >= L[m - 1][n]) 
        { 
            set<string> tmp = findLCS(X, Y, m, n - 1); 

            // merge two sets if L[m-1][n] == L[m][n-1] 
            // Note s will be empty if L[m-1][n] != L[m][n-1] 
            s.insert(tmp.begin(), tmp.end()); 
        } 
    } 
     return s; 
} 

【问题讨论】:

  • 您可以为 C 编写自己的集合。AFAIK 集合是某种树。
  • @ThomasSablik 我很想 :( 谢谢。你能给我一个关于如何以数组形式执行插入的提示吗?
  • 您真的需要一个集合还是可以使用 unordered_set?一个集合通常实现为red-black-tree。 unordered_set 通常被实现为某种哈希映射。
  • @ThomasSablik unordered 也很好,我认为。
  • 创建一个包含keynext 的结构node。创建一个指向nodes 的指针数组。使用NULL 初始化所有元素。创建一个哈希函数,该函数创建一个字符串的整数。数组中的元素是桶。每个桶都包含一个带有元素的链表。数组的大小就是桶的个数。

标签: c++ arrays string


【解决方案1】:

这是一个使用数组的自制C unordered_set示例。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define Buckets 1000

struct Node {
    char *key;
    struct Node *next;
};

void initNode(struct Node **node, const char *str) {
    *node = (struct Node *) malloc(sizeof(struct Node));
    size_t l = strlen(str);
    (*node)->key = (char *) malloc(l * sizeof(char));
    strncpy((*node)->key, str, l);
    (*node)->next = NULL;
}

void freeNode(struct Node *node) {
    if (node->next) {
        freeNode(node->next);
    }
    free(node->key);
    free(node);
}

struct Set {
    struct Node *buckets[Buckets];
};

void initSet(struct Set *set) {
    for (unsigned int i = 0; i < Buckets; ++i) {
        set->buckets[i] = NULL;
    }
}

void freeSet(struct Set *set) {
    for (unsigned int i = 0; i < Buckets; ++i) {
        if (set->buckets[i]) {
            free(set->buckets[i]);
        }
    }
}

unsigned int hash(const char *str) {
    unsigned int sum = 0;
    for (; *str; ++str) {
        sum += *str;
    }
    return sum % Buckets;
}

int insert(struct Set *set, const char *str) {
    const unsigned int h = hash(str);
    if (!set->buckets[h]) {
        initNode(&set->buckets[h], str);
        return 1;
    }

    struct Node *node = set->buckets[h];
    while (node->next && strcmp(str, node->key)) node = node->next;
    if (!strcmp(str, node->key)) return 0;
    initNode(&node->next, str);
    return 1;
}

int main() {
    struct Set set;
    initSet(&set);
    printf("%d", insert(&set, "Text"));
    printf("%d", insert(&set, "Text2"));
    printf("%d", insert(&set, "Text"));
    freeSet(&set);
}


【讨论】:

  • 谢谢。我认为这对我编写程序有很大帮助!只是一个问题,在这种情况下如何执行插入? s.insert(tmp.begin(), tmp.end());我们必须在另一个集合容器中插入范围 [position1, last) 中的所有元素。
  • C 中没有类也没有方法。因此我写了一个函数insert。您可以添加一个修改后的函数,该函数接受两个指针并插入所有元素。
猜你喜欢
  • 2013-10-15
  • 1970-01-01
  • 2018-10-31
  • 2021-06-22
  • 1970-01-01
  • 1970-01-01
  • 2019-08-26
  • 2016-12-28
  • 1970-01-01
相关资源
最近更新 更多