예제 #1
0
파일: test.c 프로젝트: ifrouter/algorithm
int main()
{
	char *text = "ababababca";
	char *s = "ababca";
	char *result = NULL;
	int i;

	KMP_MATCH *pkmp = kmp_init(s, strlen(s));
	if (pkmp == NULL)
	{
		return 0;
	}


	result = kmp_match(text, strlen(text), pkmp);
	if (result)
	{
		printf("found: offset is %d\n", result-text);
	}
	else
	{
		printf("not found\n");
	}

	kmp_free(pkmp);
	return 0;
}
예제 #2
0
파일: zutil.c 프로젝트: Chenxofhit/zimg
/**
 * @brief kmp The kmp algorithm.
 *
 * @param matcher The buffer.
 * @param mlen Buffer length.
 * @param pattern The pattern.
 * @param plen Pattern length.
 *
 * @return The place of pattern in buffer.
 */
int kmp(const char *matcher, int mlen, const char *pattern, int plen)
{
    // this function does string matching using the KMP algothm.
    // matcher and pattern are pointers to BINARY sequencies, while mlen
    // and plen are their lengths respectively.
    // if a match is found, return its position immediately.
    // return -1 if no match can be found.

    if(!mlen || !plen || mlen < plen) // take care of illegal parameters
        return -1;

    kmp_init(pattern, plen);  // prefix-function

    int i=0, j=0;
    while(i < mlen && j < plen)  // don't increase i and j at this level
    {
        if(matcher[i+j] == pattern[j])
            j++;
        else if(j == 0)  // dismatch: matcher[i] vs pattern[0]
            i++;
        else      // dismatch: matcher[i+j] vs pattern[j], and j>0
        {
            i = i + j - pi[j-1];  // i: jump forward by (j - pi[j-1])
            j = pi[j-1];          // j: reset to the proper position
        }
    }
    if(j == plen) // found a match!!
    {
        return i;
    }
    else          // if no match was found
        return -1;
}
예제 #3
0
    tuple<int, int> match(int n) {
        kmp_init(n, vr);
        for (int i = 0; i < n; ++i) {
            vs[i + n] = vs[i];
        }
        // print("next: ");
        // for (int i = 0; i < n; ++i) {
        //     print("{} ", next[i]);
        // }
        // print("\n");

        for (int i = 0, j = -1; i < n * 2; ++i) {
            for (; j > -2 && vs[i] != vr[j + 1]; j = j == -1 ? -2 : next[j]);
            ++j;
            if (j == n - 1) {
                // print("i = {}, j = {}, nx = {}\n", i, j, next[n - 1]);
                int period = n - next[n - 1] - 1;
                if (n % period != 0) period = n;
                return {period, (1 + i % period) % period};
            }
        }
        return {0, 0};
    }
예제 #4
0
파일: wstream.c 프로젝트: AbelTian/neovim
/// Initialize pools for reusing commonly created objects
void wstream_init(void)
{
  wrequest_pool = kmp_init(WRequestPool);
  wbuffer_pool = kmp_init(WBufferPool);
}
예제 #5
0
// Initialise the hash tables that are needed
void initHashes() {
	csPool = kmp_init(csPool);
	clientStatuses = kh_init(clientStatuses); // Malloc the hash
	queue = kh_init(queue);
}