With * and? Two implementation methods of the KMP algorithm

Source: Internet
Author: User

Both of them were completed by two students in the class where I was a TA. They thought they were doing well, so they posted them for sharing.

Implementation of the first student:

# Include <iostream. h>
# Include <string. h>
# Include <assert. h>
Class string
{
PRIVATE:
Char * STR;
Int size;
Public:
String (char * s );
~ String ();
 
 
Int strlen () {return strlen (this-> Str);} // evaluate the length
Char & operator [] (int I) {return STR [I];} // overload subscript operator
};

String: string (char * s)
{
Size = strlen (s );
STR = new char [size + 1];
Assert (STR! = NULL );
Strcpy (STR, S );
}
String ::~ String ()
{
Delete STR;
}
Int * nature (string & P)
{
Int Len = P. strlen ();
Assert (LEN> 0 );
Int * n = new int [Len];
Assert (n! = NULL );
If (P [0] = '*')
N [0] =-1;
Else
N [0] = 0;
Int m = 0;
Int I = 1;
While (M <Len)
{
While (I <Len & P [I]! = '*')
{
Int K = N [I-1];
If (P [I] = '? '| P [K + M] = '? ')
N [I] = k + 1;
Else
{
While (k> 0 & P [I]! = P [K + M] & P [K + M]! = '? ')
K = N [k-1];
If (P [I] = P [K + M] | P [K + M] = '? ')
N [I] = k + 1;
Else
N [I] = 0;
}
I ++;
}
While (P [I] = '*')
{
N [I] =-1;
I ++;
}
M = I;
}
Return N;
}

Int KMP (string & target, string & Pat, int * n, int startindex)
{
Int I = 0, Count = 0;
For (; I <Pat. strlen (); I ++)
If (Pat [I] = '*')
Count ++;
Int lastindex = target. strlen ()-pat. strlen ();
If (lastindex-startindex) + count <0)
Return (-1 );
I = startindex;
Int J = 0, m = 0;

While (N [J] =-1)
{
J ++;
M ++;
}
Int n, k = 0; // N and K are used to record the locations where matching is successful.
While (I <target. strlen ())
{
While (target [I]! = Pat [J] & Pat [J]! = '? '& J> m)
J = N [J-1] + m;
If (Pat [J] = target [I] | Pat [J] = '? ')
{
K ++;
If (k = 1)
N = I;

J ++;
M ++;
}


While (Pat [J] = '*')
{
J ++;
M ++;
}


If (j = Pat. strlen ())
{
Cout <"matched successfully at location" <n <"<Endl;
Return N;
}
I ++;
}
Cout <"Mismatch failed" <Endl;
Return-1;
 
}
Void main ()
{
String P ("* *? G? ");
Int * n = nature (P );
For (INT I = 0; I <p. strlen (); I ++)
Cout <n [I] <"";
Cout <Endl;
String T ("zhangg xiexzi afasdg ");
Int A = KMP (T, P, nature (P), 0 );
 
 
}

Implementation of the second Student:

Class string
{
Public:
String ();
String (char * s );
~ String ();
Char * replace (char a, char B );
Int * Next (string P );
Int kmp_findpat (string S, string P, int * n, int startindex );
Bool print ();
Protected:
Int size;
Char * STR;
};

# Include <iostream. h>
# Include <string. h>
// # Include <stdlib. h>
# Include "string. H"
# Include <assert. h>

String: string ()
{
Size = 0;
STR = NULL;
}

String: string (char * s)
{
Assert (s! = NULL );
STR = new char [strlen (s) + 1];
Assert (STR );
Strcpy (STR, S );
Size = strlen (STR );

}
// Destructor
String ::~ String ()
{
Size = 0;
}

Char * string: Replace (char a, char B)
{
For (INT I = 0; I <size; I ++)
{
If (STR [I] =)
STR [I] = B;
}
Return STR;
}

Bool string: Print ()
{
For (INT I = 0; I <size; I ++)
{
Cout <STR [I];
}
Cout <Endl;
Return true;
}

Int * string: Next (string P)
{
Int M = strlen (P. Str );
Assert (M> 0 );
Int * n = new int [m];
N [0] = 0;
Assert (n! = 0 );
For (INT I = 1; I <m; I ++)
{
Int K = N [I-1];
While (P. Str [I]! = P. Str [k]! = '? '! = '*' & K! = 0)
{
K = N [k-1];
}
If (P. Str [I] = P. Str [k] | P. Str [k] = '? '| P. Str [I] = '? '| P. Str [k] =' * '| P. Str [I] = '*')
{
N [I] = k + 1;
}
Else
{
N [I] = 0;
}
}
For (Int J = 0; j <m; j ++)
{
Cout <n [J] <"";
}
Cout <Endl;
Cout <"the length of P:" <strlen (P. Str) <Endl;
Return N;
 
}

Int string: kmp_findpat (string S, string P, int * n, int startindex)
{
// Int I;
Int m = 0;
Int J = 0;
Int lastindex = strlen (S. Str)-strlen (P. Str );
If (lastindex <startindex)
{
Return (-1 );
}
For (INT I = startindex; I <strlen (S. Str); I ++)
{
While (S. Str [I]! = P. Str [J] & J> 0 & P. Str [J]! = '*' & P. Str [J]! = '? ')
{
J = N [J-1];
}
If (P. Str [J] = '*')
{
J = J + 1;
While (S. Str [I + 1]! = P. Str [J] & I <strlen (S. Str ))
{
I ++;
M ++;
}
If (I = strlen (S. Str ))
{
Return-1;
}
}
If (S. Str [I] = P. Str [J] | P. Str [J] = '? ')
{
J ++;
}

If (j = strlen (P. Str ))
{
Return (I-j + 1-(S-1 ));
}
}
Return (-1 );
}

# Include "string. H"
# Include <iostream. h>
# Include <string. h>
Void main ()
{
Int * N;
Int N;
// Char * P = "infinite ";
// Char * s = "infinishinfinfinite ";
String P ("A * D * F ");
String S ("sddddasdfhhhjf ");
P. Print ();
S. Print ();
N = P. Next (P );
N = S. kmp_findpat (S, P, N, 0 );
Cout <n <Endl;
 
/* For (INT I = N; I <n + p. strlen (); I ++)
{
Cout <S. Str [I];
}*/
Cout <Endl;

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.