KMP string pattern matching (25 minutes)

Given two strings and String Pattern of the English alphabet, it requires finding the position of the first occurrence of Pattern in a String, String and after the sub-string output this location. If not, then output "Not Found".

This question is designed to test a variety of different matching algorithm performance data in a variety of situations. Each set of test data the following features:

  • Data 0: a small string, test basic correctness;
  • 1 data: random data, String length is  1, Pattern length is  1;
  • 2 data: random data, String length is  1, Pattern length is  1;
  • 3 data: random data, String length is  1, Pattern length is  1;
  • Data 4: random data, String length is  1, Pattern length is  1;
  • Data 5: String length is  1, Pattern length is  1; the last character in the case of the test do not match;
  • Data 6: String length is  1, Pattern length  1; test case match the first characters do not.

Input formats:

The first row gives the input String, English letters by the length of not more than  . 1 0 ^ 6  string. The second line gives a positive integer  N ( . 1 0) , the number of pattern string to be matched. Then  N rows, each row is given a the Pattern, English letters by the length of not more than  . 1 0 ^ 5  string. Each string is not empty, ending with a carriage return.

Output formats:

Each Pattern, in accordance with the required output face title matching result.

Sample input:

abcabcabcabcacabxy
3
abcabcacab
cabcabcd
abcabcabcabcacabxyz

Sample output:

abcabcacabxy
Not Found
Not Found

#include<cstdio>
#include<cstring>
#include<cstdlib>
const int maxn = 1000100;

int KMP(char *string, char *pattern);
void buildMatch(char *pattern, int *match);

int main ()
{
    char string[maxn] = {0};
    char pattern[maxn] = {0};
    int n;
    
    scanf("%s", &string);
    scanf("%d", &n);
    
    for (int i = 0; i < n; i++)
    {
        scanf("%s", &pattern);
        int p = KMP(string, pattern);
        if (p != -1)
        {
            printf("%s", string+p);
        }
        else
        {
            printf("Not Found");
        }
        
        if (i < n-1)
        {
            printf("\n");
        }
    }
    
    return 0;
}

int KMP ( char * to a string , char * pattern)
{
    int n = strlen(string);
    int m = strlen(pattern);
    
    if (n < m)
    {
        return -1;
    }
    int *match = (int *)malloc(sizeof(int) * m);
    
    buildMatch(pattern, match);
    
    int s = 0;
    int p = 0;
    while (s < n && p < m)
    {
        if (string[s] == pattern[p])
        {
            s++;
            p++;
        }
        else if(p > 0)
        {
            p = match[p-1] + 1;
        }
        else
        {
            s++;
        }
    }
    
    return (p == m) ? (s - m) : -1;
}

void buildMatch(char *pattern, int *match)
{
    int m = strlen(pattern);
    match[0] = -1;
    
    for (int j = 1; j < m; j++)
    {
        int i = match[j-1];
        while( (i >= 0) && (pattern[i + 1] != pattern[j]) )
        {
            i = match[i];
        }
        
        if (pattern[i + 1] == pattern[j])
        {
            match[j] = i + 1;
        }
        else
        {
            match [j] = - 1 ;
        }
    }
}

 

 

Guess you like

Origin www.cnblogs.com/wanghao-boke/p/12006005.html