74_kmp.sy 1.01 KB
Newer Older
龚平's avatar
init  
龚平 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

void get_next(int str[], int next[])
{
    next[0] = -1;
    int i = 0, j = -1;
    while (str[i]) {
        if (j == -1 || str[i] == str[j]) {
            j = j + 1;
            i = i + 1;
            next[i] = j;
        }
        else
            j = next[j];
    }
}

int KMP(int dst[], int src[])
{
    int next[4096];
    get_next(dst, next);

    int i = 0, j = 0;
    while (src[j]) {
        if (dst[i] == src[j]) {
            i = i + 1;
            j = j + 1;
            if (!dst[i]) {
                return j;
            }
        } else {
            i = next[i];
            if (i == -1) {
                i = i + 1;
                j = j + 1;
            }
        }
    }
    return -1;
}

int read_str(int buf[])
{
    int i = 0;
    while (1) {
        buf[i] = getch();
        if (buf[i] == 10)
            break;
        i = i + 1;
    }
    buf[i] = 0;
    return i;
}

int main()
{
    int dst[4096], src[4096];
    read_str(dst);
    read_str(src);
    putint(KMP(dst, src));
    putch(10);
    return 0;
}