string check

Description: Some high-level programming languages ​​today have more and more powerful support for string processing (such as Java, Perl, etc.), but string search itself is still a topic worthy of discussion. Here, the Boyer-Moore method is used to illustrate how For string description, this method is fast and the principle is concise and easy to understand.
 

Solution: String search itself is not difficult, and brute force can also be used to solve the problem, but how to quickly search for a string is not easy. Traditional string search starts from the keyword and the beginning of the string, such as Knuth-Morris- Pratt's algorithm is used to search for strings. This method is also good, but it takes time to calculate the formula; Boyer-Moore string checking is changed to check the string after the keyword, and make a progress table. Advance the value in the advance table to the next checkpoint, assuming p is OK, then

Whether the value of p-n+1 to p in the post-comparison string is the same as the keyword. If there are repeated characters in the keyword, the advance value will have more than two values. In this case, the value with the smaller advance value will be taken, so that possible positions will not be skipped. For example, the keyword texture, The advance value of t should take the following 3 instead of the preceding 7.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void table(char*); // 建立前进表
int search(int, char*, char*); // 搜寻关键字
void substring(char*, char*, int, int); // 取出子字串
int skip[256];
int main(void) {
    char str_input[80];
    char str_key[80];
    char tmp[80] = {'\0'};
    int m, n, p;
    printf("请输入字串:");
    gets(str_input);
    printf("请输入搜寻关键字:");
    gets(str_key);
    m = strlen(str_input); // 计算字串长度
    n = strlen(str_key);
    table(str_key);
    p = search(n-1, str_input, str_key);
    while(p != -1) {
        substring(str_input, tmp, p, m);
        printf("%s\n", tmp);
        p = search(p+n+1, str_input, str_key);
    }
    printf("\n");
    return 0;
}
void table(char *key) {
    int k, n;
    n = strlen(key);
    for(k = 0; k <= 255; k++) skip[k] = n;
    for(k = 0; k < n - 1; k++) skip[key[k]] = n - k - 1;
}
int search(int p, char* input, char* key) {
    int i, m, n;
    char tmp[80] = {'\0'};
    m = strlen(input);
    n = strlen(key);
    while(p < m) {
        substring(input, tmp, p-n+1, p);
        if(!strcmp(tmp, key)) // 比较两字串是否相同
            return p-n+1;
        p += skip[input[p]];
    }
    return -1;
}
void substring(char *text, char* tmp, int s, int e) {
    int i, j;
    for(i = s, j = 0; i <= e; i++, j++) mp[j] = text[i];
    tmp[j] = '\0';
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324505791&siteId=291194637