首页 > 其他分享 >模糊匹配

模糊匹配

时间:2024-03-03 22:35:28浏览次数:19  
标签:current 匹配 scan 模糊 char entry path hash

最近工作中需要实现一个模糊匹配的功能,这里记录一下实现方式:

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

#define HASH_SIZE 1000 // 哈希表大小,根据实际情况调整

typedef struct Node {
    char filename[256]; // 文件名最大长度
    char path[1024];    // 文件路径
    struct Node *next;
} Node;

Node *hash_table[HASH_SIZE];

unsigned int hash(const char *str) {
    unsigned int hash = 5381;
    int c;

    while ((c = *str++)) {
        hash = ((hash << 5) + hash) + c;
    }

    return hash % HASH_SIZE;
}

void insert_to_hash(const char *filename, const char *path) {
    unsigned int index = hash(filename);

    Node *new_node = (Node *)malloc(sizeof(Node));
    strcpy(new_node->filename, filename);
    strcpy(new_node->path, path);
    new_node->next = hash_table[index];
    hash_table[index] = new_node;
}

void search_in_hash(const char *keyword) {
    for (int i = 0; i < HASH_SIZE; i++) {
        Node *current = hash_table[i];
        while (current != NULL) {
            if (strstr(current->filename, keyword) != NULL) {
                printf("Matching file found: %s\n", current->path);
            }
            current = current->next;
        }
    }
}

void scan_audio_files(const char *path) {
    DIR *dir;
    struct dirent *entry;

    if ((dir = opendir(path)) == NULL) {
        perror("opendir");
        exit(EXIT_FAILURE);
    }

    while ((entry = readdir(dir)) != NULL) {
        char full_path[1024];
        snprintf(full_path, sizeof(full_path), "%s/%s", path, entry->d_name);

        if (entry->d_type == DT_REG) {
            insert_to_hash(entry->d_name, full_path); // 将文件名和路径插入哈希表中
        } else if (entry->d_type == DT_DIR && strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
            scan_audio_files(full_path);
        }
    }

    closedir(dir);
}

int main() {
    const char *directory_to_scan = "/home/lx/linux_c/scan_file/scan_file"; // 替换为你要扫描的目录路径
    scan_audio_files(directory_to_scan);

    // 在这里实现用户输入关键字进行模糊匹配检索
    char keyword[100];
    printf("Enter keyword: ");
    scanf("%s", keyword);

    search_in_hash(keyword);

    return 0;
}

输出如下:

标签:current,匹配,scan,模糊,char,entry,path,hash
From: https://www.cnblogs.com/lx2035/p/18050893

相关文章

  • sql语句in的用法(使用多个列进行匹配)
    原文链接:https://www.cnblogs.com/hellohui/p/17670858.html今天遇到了in的新用法,即文中的第四条,使用多个列进行匹配,在此记录下。使用列值列表进行匹配:SELECT*FROM表名WHERE列名IN(值1,值2,值3,...)使用子查询返回多个结果进行匹配:SELECT*FROM表名WHERE......
  • 局部匹配的查找
    问题:存在包含指定内容的返回“是”,否则返回“否”。函数公式解决:查找范围为同行:=IF(COUNTIF(B2,"*"&A2&"*"),"是","否")查找范围不限定行:=IF(COUNTIF(B:B,"*"&A2&"*"),"是","否")CountIf的条件使用前后都带通配符星号,表示“包含”。 ......
  • 牛客练习赛122 F 括号匹配 费用流
    CF打多了很多题目中的性质都挖掘出来了,也想到了费用流。很难\(dp\)因为一组中三个括号留下来一个很难作为状态进行dp。由于对括号匹配还不熟悉以为是\(n^2\)的图就没写了,事实上应该是线性的建图。所以对于\(n=2000\)这个数据范围网络流是可以过的。设置源点\(S\)和汇点\(T\)。......
  • 通过正则表达式匹配PNR编码中的航班信息
    PNR编辑内容主要由文本信息构,结果如下:一种方式是通过分隔符来提取信息,但效果是不理想的,因为格式会出现变化,推荐的方式是使用正则表达式,例如:1、通过正则获取航班号stringstrPattern=@"^[\s|\+|\-]?(?<Index>\d+)\.\s+\*?(?<FlightNo>\*?[A-Z0-9]{5,7})\s*(?<Level>[A-Z])\d......
  • kmp模式匹配例题思考
    题目描述读入一个字符串数组string[],再读入一个短字符串。要求查找string[]中和短字符串的所有匹配,输出行号和匹配的字符串以。匹配时不区分大小写,并且可以有一个中括号表示的模式匹配。例如,对aa[123]bb来说,aa1bb,aa2bb,aa3bb都算匹配。输入格式:第一行输入字符串数组的长度接下......
  • 关于KMP模式匹配的一些思考
    算法简介模式匹配给定主串text和模式串pattern,在主串中查找,如果找到了模式串,返回模式串在主串中的起始位置,从1开始计数。暴力求解求解模式匹配算法的核心思想是:蛮力法。即使用两个指针i和j,其中i指针用来遍历text,j指针用来遍历pattern。当text[i]==text[j]的时候,继续比较;如果不......
  • 写给rust初学者的教程(一):枚举、特征、实现、模式匹配
    这系列RUST教程一共三篇。这是第一篇,介绍RUST语言的入门概念,主要有enum\trait\impl\match等语言层面的东西。安装好你的rust开发环境,用cargo创建一个空项目,咱们直接上代码。懵逼的同僚可以参考我8年前的rust文章:https://www.iteye.com/blog/somefuture-2275494,虽然8年了,然并不......
  • 代码随想录算法训练营day09 | leetcode 28. 找出字符串中第一个匹配项的下标、459. 重
    目录题目链接:28.找出字符串中第一个匹配项的下标-简单题目链接:459.重复的子字符串-简单题目链接:28.找出字符串中第一个匹配项的下标-简单题目描述:给你两个字符串haystack和needle,请你在haystack字符串中找出needle字符串的第一个匹配项的下标(下标从0开始)。如果ne......
  • 特征匹配
    案例1importcv2importnumpyasnpimportmatplotlib.pyplotasplt%matplotlibinlineimg1=cv2.imread('01_Picture/19_Box.png',0)img2=cv2.imread('01_Picture/20_Box_in_scene.png',0)defcv_show(name,img):cv2.imshow(name,img)......
  • vim括号匹配等跳转技巧
    %跳转到相配对的括号gD跳转到局部变量的定义处''跳转到光标上次停靠的地方,是两个',而不是一个"mx设置书签,x只能是a-z的26个字母,"`x"跳转到书签处>增加缩进,"x>"表示增加以下x行的缩进<减少缩进,"x<"表示减少以下x行的缩进{跳到上一段的开头}跳到下一段的的......