1.串的顺序存储
#define MAXLEN 255
typedef struct
{
char ch[MAXILEN+1];
int length;
}SString;
typedef struct
{
char *ch;
int length;
}HString;
2.串的链式存储结构
#define CHUNKSIZE 80
typedef struct Chunk{
char ch[CHUNKSIZE];
struct Chunk *next;
}Chunk;
typedef struct{
Chunk *head,*tail;
int curlen;
}LString;
3.BF算法
int index(SString S,SString T,int pos)
{
i = pos; j = 1;
while(i<=S.length && j<=T.length)
{
if(S.ch[i] == T.ch[j])
{
++i;++j;
}
else
{
i=i-j+1;
j=1;
}
if ( j>T.length)
return i-T.length;
else
return 0;
}
}
4.KMP算法
int Index_KMP(SString S,SString T,int pos)
{
i = pos; j = 1;
while(i<=S.length && j<=T.length)
{
if(j==0 || S.ch[i]==T.ch[j])
{
++i;++j
}
else
j = next[j];
}
if(j>T.length)
return i-T.length;
else
return 0;
}
void get_next(SString P,int &next[])
{
j = 1; next[j] = 0; k = 0;
while(j<P.length)
{
if(k==0 || P.ch[j] == P.ch[k])
{
++j;++k;
next[j] = k;
}
else
k = next[k];
}
}
void get_nextval(SString P,int &nextval[])
{
j = 1; nextval = 0; k = 0;
while(j<P.length)
{
if(k==0 || P.ch[j] == P.ch[k])
{
++j;++k;
if(P.ch[j] != P.ch[k])
nextval[j] = k;
else
nextval[j] = nextval[k];
}
else
k = nextval[k];
}
}