Использование массива вместо набора <string> - PullRequest
0 голосов
/ 04 апреля 2020

Это функция для нахождения всех самых длинных общих последовательностей для последовательностей X и Y. Но эта программа на c ++, но я хочу написать ее на C.

Есть ли способ использовать массив вместо набора?

Например. если входное значение равно

X = < A, A, T, C, C, >

Y = < A, C, A, C, G, >

, то выходное значение должно быть

< A, C, C, >

< A, A, C, >

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

1 Ответ

0 голосов
/ 05 апреля 2020

Вот пример для самодельного 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);
}


Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...