中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

c語言字符串匹配算法怎么寫

小億
91
2024-01-03 10:09:41
欄目: 編程語言

C語言字符串匹配算法有很多種,下面介紹幾種常用的算法實現。

  1. Brute-Force算法(樸素算法)
int strStr(char* haystack, char* needle) {
    int i, j;
    int len1 = strlen(haystack);
    int len2 = strlen(needle);
    
    for (i = 0; i <= len1 - len2; i++) {
        for (j = 0; j < len2; j++) {
            if (haystack[i + j] != needle[j]) {
                break;
            }
        }
        if (j == len2) {
            return i;
        }
    }
    
    return -1;
}
  1. KMP算法(Knuth-Morris-Pratt算法)
void getNext(char* needle, int* next) {
    int i, j;
    int len = strlen(needle);
    
    next[0] = -1;
    i = 0;
    j = -1;
    while (i < len) {
        if (j == -1 || needle[i] == needle[j]) {
            i++;
            j++;
            next[i] = j;
        } else {
            j = next[j];
        }
    }
}

int strStr(char* haystack, char* needle) {
    int i, j;
    int len1 = strlen(haystack);
    int len2 = strlen(needle);
    int* next = (int*)malloc(sizeof(int) * len2);
    
    getNext(needle, next);
    
    i = 0;
    j = 0;
    while (i < len1 && j < len2) {
        if (j == -1 || haystack[i] == needle[j]) {
            i++;
            j++;
        } else {
            j = next[j];
        }
    }
    
    free(next);
    if (j == len2) {
        return i - j;
    } else {
        return -1;
    }
}
  1. Boyer-Moore算法
#define MAX_CHAR 256

int max(int a, int b) {
    return a > b ? a : b;
}

void badCharHeuristic(char* needle, int* badChar) {
    int i;
    int len = strlen(needle);
    for (i = 0; i < MAX_CHAR; i++) {
        badChar[i] = -1;
    }
    for (i = 0; i < len; i++) {
        badChar[(int)needle[i]] = i;
    }
}

void goodSuffixHeuristic(char* needle, int* goodSuffix) {
    int i, j;
    int len = strlen(needle);
    int* suffix = (int*)malloc(sizeof(int) * (len + 1));
    
    for (i = 0; i <= len; i++) {
        suffix[i] = -1;
    }
    
    for (i = len - 1; i >= 0; i--) {
        if (suffix[i] == -1) {
            suffix[i] = len - i - 1;
            for (j = 0; j < i; j++) {
                if (needle[j] == needle[len - i + j]) {
                    suffix[j] = len - i + j;
                }
            }
        }
    }
    
    for (i = 0; i < len; i++) {
        goodSuffix[i] = len - suffix[i + 1];
    }
    for (i = 0; i <= len; i++) {
        goodSuffix[len] = len - suffix[i + 1];
    }
    
    free(suffix);
}

int strStr(char* haystack, char* needle) {
    int i = 0, j = 0;
    int len1 = strlen(haystack);
    int len2 = strlen(needle);
    int badChar[MAX_CHAR];
    int* goodSuffix = (int*)malloc(sizeof(int) * (len2 + 1));
    
    badCharHeuristic(needle, badChar);
    goodSuffixHeuristic(needle, goodSuffix);
    
    while (i <= len1 - len2) {
        j = len2 - 1;
        while (j >= 0 && needle[j] == haystack[i + j]) {
            j--;
        }
        if (j < 0) {
            free(goodSuffix);
            return i;
        } else {
            i += max(goodSuffix[j], j - badChar[(int)haystack[i + j]]);
        }
    }
    
    free(goodSuffix);
    return -1;
}

以上是幾種常用的字符串匹配算法的C語言

0
古交市| 弋阳县| 延吉市| 二连浩特市| 保德县| 翁牛特旗| 新田县| 弋阳县| 泗洪县| 饶阳县| 裕民县| 安福县| 孝义市| 江门市| 花莲市| 诸城市| 沧源| 梁山县| 天柱县| 宜宾市| 秦安县| 长垣县| 桂林市| 象山县| 建始县| 合肥市| 五莲县| 都兰县| 浑源县| 镇巴县| 尼玛县| 郁南县| 沂南县| 涡阳县| 拜泉县| 景泰县| 罗田县| 云林县| 镇平县| 萨嘎县| 棋牌|