• 复习KMP


    这波,这波是上手进行康复训练

    char s[MAXN], s1[MAXN];
    int nex[MAXN];
    
    void make_next() {
        int i = 0, j = -1;
        int len = strlen(s);
        nex[0] = -1;
        while(i < len) {
            if(j == -1 || s[i] == s[j]) {
                i++;
                j++;
                nex[i] = j;
            }
            else j = nex[j];
        }
    }
    
    void out_next() {
        int len = strlen(s);
        for(int i = 0; i < len; i++) {
            cout << nex[i] << endl;
        }
    }
    
    void kmp() {
        make_next();
        int i = 0, j = 0;
        int len = strlen(s1), lens = strlen(s);
        while(i < len) {
            if(j == -1 || s[j] == s1[i]) {
                j++;
                i++;
            }
            else j = nex[j];
            if(j == lens) {
                cout << i << endl;
                break;
            }
        }
    }
    
    int main() {
        cin >> s;
        cin >> s1;
        kmp();
        return 0;
    }
    
  • 相关阅读:
    utils04_搭建私有Git服务器
    utils03_clone远程仓库
    java 22
    java 22
    java 22
    java 22
    java 22
    java 22
    java 22
    java 22
  • 原文地址:https://www.cnblogs.com/ASLHZXY/p/14532642.html
Copyright © 2020-2023  润新知