主题:[讨论]串匹配的原理求教
简单串匹配
这个能看懂:
#include "stdio.h"
#include <string.h>
int nfind ( char *B, char *A )
{
int i, j, start = 0;
int lasts = strlen (B ) -1;
int lastp = strlen ( A) -1 ;
int endmatch = lastp;
for(i=0; endmatch<=lasts; endmatch++, start++) {
if ( B[endmatch] == A[lastp ])
for ( j=0, i=start; j<lastp && B[i]==A[j]; i++,j++)
;
if ( j == lastp )
return start; /*成功 */
}
return -1;
}
void main ()
{
char s[]="Sit please.";
char t[]="please ";
int po=nfind(s,t);
printf("%d",po);
}
改进串匹配:
#include "stdio.h"
#include <string.h>
#include <stdio.h>
#include <String.h>
#define max_sring_size 100
#define max_Pattern_size 100
int Failure[max_Pattern_size];
void fail(char *Pat )
{
int i,j,n=strlen(Pat);
Failure [ 0 ] = -1;
for ( j = 1; j < n; j ++) {
i = Failure [ j -1 ];
while ((Pat [ j ] != Pat [ i +1 ])&& ( i >= 0))
i = Failure [ i ];
if ( Pat[j] == Pat [ i + 1] )
Failure[j]= i + 1;
else
Failure [j] = -1;
}
}
int match ( char *String, char *Pat)
{
int i = 0, j = 0;
int lens = strlen ( String );
int lenp = strlen ( Pat);
while ( i < lens && j <lenp ) {
if (String [i] == Pat[j] ){
i ++;
j ++;
}
else if ( j== 0 )
i ++;
else
j=Failure [j-1] + 1;
}
return (( j == lenp ) ? ( i - lenp ) : -1 );
}
void main ()
{
char s[]="Sit please.";
char t[]="please ";
int po=match(s,t);
printf("%d",po);
}
这个何解,哪位大虾能仔细的说一下改进串匹配的原理?小弟万分感谢!
这个能看懂:
#include "stdio.h"
#include <string.h>
int nfind ( char *B, char *A )
{
int i, j, start = 0;
int lasts = strlen (B ) -1;
int lastp = strlen ( A) -1 ;
int endmatch = lastp;
for(i=0; endmatch<=lasts; endmatch++, start++) {
if ( B[endmatch] == A[lastp ])
for ( j=0, i=start; j<lastp && B[i]==A[j]; i++,j++)
;
if ( j == lastp )
return start; /*成功 */
}
return -1;
}
void main ()
{
char s[]="Sit please.";
char t[]="please ";
int po=nfind(s,t);
printf("%d",po);
}
改进串匹配:
#include "stdio.h"
#include <string.h>
#include <stdio.h>
#include <String.h>
#define max_sring_size 100
#define max_Pattern_size 100
int Failure[max_Pattern_size];
void fail(char *Pat )
{
int i,j,n=strlen(Pat);
Failure [ 0 ] = -1;
for ( j = 1; j < n; j ++) {
i = Failure [ j -1 ];
while ((Pat [ j ] != Pat [ i +1 ])&& ( i >= 0))
i = Failure [ i ];
if ( Pat[j] == Pat [ i + 1] )
Failure[j]= i + 1;
else
Failure [j] = -1;
}
}
int match ( char *String, char *Pat)
{
int i = 0, j = 0;
int lens = strlen ( String );
int lenp = strlen ( Pat);
while ( i < lens && j <lenp ) {
if (String [i] == Pat[j] ){
i ++;
j ++;
}
else if ( j== 0 )
i ++;
else
j=Failure [j-1] + 1;
}
return (( j == lenp ) ? ( i - lenp ) : -1 );
}
void main ()
{
char s[]="Sit please.";
char t[]="please ";
int po=match(s,t);
printf("%d",po);
}
这个何解,哪位大虾能仔细的说一下改进串匹配的原理?小弟万分感谢!