mirror of
https://github.com/by777/dataStructureForC.git
synced 2026-02-02 17:48:51 +08:00
KMP完善
This commit is contained in:
@@ -1,262 +0,0 @@
|
||||
#include "string.h"
|
||||
#include "stdio.h"
|
||||
#include "stdlib.h"
|
||||
#include "io.h"
|
||||
#include "math.h"
|
||||
#include "time.h"
|
||||
|
||||
#define OK 1
|
||||
#define ERROR 0
|
||||
#define TRUE 1
|
||||
#define FALSE 0
|
||||
#define MAXSIZE 100 /* <20>洢<EFBFBD>ռ<EFBFBD><D5BC><EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
|
||||
typedef int Status; /* Status<75>Ǻ<EFBFBD><C7BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><>ֵ<EFBFBD>Ǻ<EFBFBD><C7BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD>룬<EFBFBD><EBA3AC>OK<4F><4B> */
|
||||
typedef int ElemType; /* ElemType<70><65><EFBFBD><EFBFBD><CDB8><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊint */
|
||||
|
||||
typedef char String[MAXSIZE + 1]; /* 0<>ŵ<EFBFBD>Ԫ<EFBFBD><D4AA><EFBFBD>Ŵ<EFBFBD><C5B4>ij<EFBFBD><C4B3><EFBFBD> */
|
||||
|
||||
/* <20><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>chars<72>Ĵ<EFBFBD>T */
|
||||
Status StrAssign(String T, char *chars)
|
||||
{
|
||||
int i;
|
||||
if (strlen(chars) > MAXSIZE)
|
||||
return ERROR;
|
||||
else
|
||||
{
|
||||
T[0] = strlen(chars);
|
||||
for (i = 1; i <= T[0]; i++)
|
||||
T[i] = *(chars + i - 1);
|
||||
return OK;
|
||||
}
|
||||
}
|
||||
|
||||
Status ClearString(String S)
|
||||
{
|
||||
S[0] = 0; /* <20><EFBFBD><EEB4AE>Ϊ<EFBFBD><CEAA> */
|
||||
return OK;
|
||||
}
|
||||
|
||||
/* <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>T<EFBFBD><54> */
|
||||
void StrPrint(String T)
|
||||
{
|
||||
int i;
|
||||
for (i = 1; i <= T[0]; i++)
|
||||
printf("%c", T[i]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
/* <20><><EFBFBD><EFBFBD>Next<78><74><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5> */
|
||||
void NextPrint(int next[], int length)
|
||||
{
|
||||
int i;
|
||||
for (i = 1; i <= length; i++)
|
||||
printf("%d", next[i]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
/* <20><><EFBFBD>ش<EFBFBD><D8B4><EFBFBD>Ԫ<EFBFBD>ظ<EFBFBD><D8B8><EFBFBD> */
|
||||
int StrLength(String S)
|
||||
{
|
||||
return S[0];
|
||||
}
|
||||
|
||||
/* <20><><EFBFBD>ص<EFBFBD>ģʽƥ<CABD>䷨ */
|
||||
int Index(String S, String T, int pos)
|
||||
{
|
||||
int i = pos; /* i<><69><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>S<EFBFBD>е<EFBFBD>ǰλ<C7B0><CEBB><EFBFBD>±<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>pos<6F><73>Ϊ1<CEAA><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>posλ<73>ÿ<EFBFBD>ʼƥ<CABC><C6A5> */
|
||||
int j = 1; /* j<><6A><EFBFBD><EFBFBD><EFBFBD>Ӵ<EFBFBD>T<EFBFBD>е<EFBFBD>ǰλ<C7B0><CEBB><EFBFBD>±<EFBFBD>ֵ */
|
||||
while (i <= S[0] && j <= T[0]) /* <20><>iС<69><D0A1>S<EFBFBD>ij<EFBFBD><C4B3>Ȳ<EFBFBD><C8B2><EFBFBD>jС<6A><D0A1>T<EFBFBD>ij<EFBFBD><C4B3><EFBFBD>ʱ<EFBFBD><CAB1>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
{
|
||||
if (S[i] == T[j]) /* <20><><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
{
|
||||
++i;
|
||||
++j;
|
||||
}
|
||||
else /* ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¿<EFBFBD>ʼƥ<CABC><C6A5> */
|
||||
{
|
||||
i = i - j + 2; /* i<>˻ص<CBBB><D8B5>ϴ<EFBFBD>ƥ<EFBFBD><C6A5><EFBFBD><EFBFBD>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>һλ */
|
||||
j = 1; /* j<>˻ص<CBBB><D8B5>Ӵ<EFBFBD>T<EFBFBD><54><EFBFBD><EFBFBD>λ */
|
||||
}
|
||||
}
|
||||
if (j > T[0])
|
||||
return i - T[0];
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ͨ<><CDA8><EFBFBD><EFBFBD><EFBFBD>㷵<EFBFBD><E3B7B5><EFBFBD>Ӵ<EFBFBD>T<EFBFBD><54>next<78><74><EFBFBD>顣 */
|
||||
void get_next(String T, int *next)
|
||||
{
|
||||
int i, j;
|
||||
i = 1;
|
||||
j = 0;
|
||||
next[1] = 0;
|
||||
while (i < T[0]) /* <20>˴<EFBFBD>T[0]<5D><>ʾ<EFBFBD><CABE>T<EFBFBD>ij<EFBFBD><C4B3><EFBFBD> */
|
||||
{
|
||||
if (j == 0 || T[i] == T[j]) /* T[i]<5D><>ʾ<EFBFBD><CABE><EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>T[j]<5D><>ʾǰ<C7B0>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD>ַ<EFBFBD> */
|
||||
{
|
||||
++i;
|
||||
++j;
|
||||
next[i] = j;
|
||||
}
|
||||
else
|
||||
j = next[j]; /* <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD>jֵ<6A><D6B5><EFBFBD><EFBFBD> */
|
||||
}
|
||||
}
|
||||
|
||||
/* <20><><EFBFBD><EFBFBD><EFBFBD>Ӵ<EFBFBD>T<EFBFBD><54><EFBFBD><EFBFBD><EFBFBD><EFBFBD>S<EFBFBD>е<EFBFBD>pos<6F><73><EFBFBD>ַ<EFBFBD>֮<EFBFBD><D6AE><EFBFBD><EFBFBD>λ<EFBFBD>á<EFBFBD><C3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵΪ0<CEAA><30> */
|
||||
/* T<>ǿգ<C7BF>1<EFBFBD><31>pos<6F><73>StrLength(S)<29><> */
|
||||
int Index_KMP(String S, String T, int pos)
|
||||
{
|
||||
int i = pos; /* i<><69><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>S<EFBFBD>е<EFBFBD>ǰλ<C7B0><CEBB><EFBFBD>±<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>pos<6F><73>Ϊ1<CEAA><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>posλ<73>ÿ<EFBFBD>ʼƥ<CABC><C6A5> */
|
||||
int j = 1; /* j<><6A><EFBFBD><EFBFBD><EFBFBD>Ӵ<EFBFBD>T<EFBFBD>е<EFBFBD>ǰλ<C7B0><CEBB><EFBFBD>±<EFBFBD>ֵ */
|
||||
int next[255]; /* <20><><EFBFBD><EFBFBD>һnext<78><74><EFBFBD><EFBFBD> */
|
||||
get_next(T, next); /* <20>Դ<EFBFBD>T<EFBFBD><54><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>next<78><74><EFBFBD><EFBFBD> */
|
||||
while (i <= S[0] && j <= T[0]) /* <20><>iС<69><D0A1>S<EFBFBD>ij<EFBFBD><C4B3>Ȳ<EFBFBD><C8B2><EFBFBD>jС<6A><D0A1>T<EFBFBD>ij<EFBFBD><C4B3><EFBFBD>ʱ<EFBFBD><CAB1>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
{
|
||||
if (j == 0 || S[i] == T[j]) /* <20><><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>㷨<EFBFBD><E3B7A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>j=0<>ж<EFBFBD> */
|
||||
{
|
||||
++i;
|
||||
++j;
|
||||
}
|
||||
else /* ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¿<EFBFBD>ʼƥ<CABC><C6A5> */
|
||||
j = next[j]; /* j<>˻غ<CBBB><D8BA>ʵ<EFBFBD>λ<EFBFBD>ã<EFBFBD>iֵ<69><D6B5><EFBFBD><EFBFBD> */
|
||||
}
|
||||
if (j > T[0])
|
||||
return i - T[0];
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* <20><>ģʽ<C4A3><CABD>T<EFBFBD><54>next<78><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>nextval */
|
||||
void get_nextval(String T, int *nextval)
|
||||
{
|
||||
int i, j;
|
||||
i = 1;
|
||||
j = 0;
|
||||
nextval[1] = 0;
|
||||
while (i < T[0]) /* <20>˴<EFBFBD>T[0]<5D><>ʾ<EFBFBD><CABE>T<EFBFBD>ij<EFBFBD><C4B3><EFBFBD> */
|
||||
{
|
||||
if (j == 0 || T[i] == T[j]) /* T[i]<5D><>ʾ<EFBFBD><CABE><EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>T[j]<5D><>ʾǰ<C7B0>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD>ַ<EFBFBD> */
|
||||
{
|
||||
++i;
|
||||
++j;
|
||||
if (T[i] != T[j]) /* <20><><EFBFBD><EFBFBD>ǰ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ǰ<C7B0>ַ<EFBFBD><D6B7><EFBFBD>ͬ */
|
||||
nextval[i] = j; /* <20><><EFBFBD><EFBFBD>ǰ<EFBFBD><C7B0>jΪnextval<61><6C>iλ<69>õ<EFBFBD>ֵ */
|
||||
else
|
||||
nextval[i] = nextval[j]; /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<C7B0>ַ<EFBFBD><D6B7><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<C7B0>ַ<EFBFBD><D6B7><EFBFBD> */
|
||||
/* nextvalֵ<6C><D6B5>ֵ<EFBFBD><D6B5>nextval<61><6C>iλ<69>õ<EFBFBD>ֵ */
|
||||
}
|
||||
else
|
||||
j = nextval[j]; /* <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD>jֵ<6A><D6B5><EFBFBD><EFBFBD> */
|
||||
}
|
||||
}
|
||||
|
||||
int Index_KMP1(String S, String T, int pos)
|
||||
{
|
||||
int i = pos; /* i<><69><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>S<EFBFBD>е<EFBFBD>ǰλ<C7B0><CEBB><EFBFBD>±<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>pos<6F><73>Ϊ1<CEAA><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>posλ<73>ÿ<EFBFBD>ʼƥ<CABC><C6A5> */
|
||||
int j = 1; /* j<><6A><EFBFBD><EFBFBD><EFBFBD>Ӵ<EFBFBD>T<EFBFBD>е<EFBFBD>ǰλ<C7B0><CEBB><EFBFBD>±<EFBFBD>ֵ */
|
||||
int next[255]; /* <20><><EFBFBD><EFBFBD>һnext<78><74><EFBFBD><EFBFBD> */
|
||||
get_nextval(T, next); /* <20>Դ<EFBFBD>T<EFBFBD><54><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>next<78><74><EFBFBD><EFBFBD> */
|
||||
while (i <= S[0] && j <= T[0]) /* <20><>iС<69><D0A1>S<EFBFBD>ij<EFBFBD><C4B3>Ȳ<EFBFBD><C8B2><EFBFBD>jС<6A><D0A1>T<EFBFBD>ij<EFBFBD><C4B3><EFBFBD>ʱ<EFBFBD><CAB1>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
{
|
||||
if (j == 0 || S[i] == T[j]) /* <20><><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>㷨<EFBFBD><E3B7A8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>j=0<>ж<EFBFBD> */
|
||||
{
|
||||
++i;
|
||||
++j;
|
||||
}
|
||||
else /* ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¿<EFBFBD>ʼƥ<CABC><C6A5> */
|
||||
j = next[j]; /* j<>˻غ<CBBB><D8BA>ʵ<EFBFBD>λ<EFBFBD>ã<EFBFBD>iֵ<69><D6B5><EFBFBD><EFBFBD> */
|
||||
}
|
||||
if (j > T[0])
|
||||
return i - T[0];
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int i, *p;
|
||||
String s1, s2;
|
||||
|
||||
StrAssign(s1, "abcdex");
|
||||
printf("<EFBFBD>Ӵ<EFBFBD>Ϊ: ");
|
||||
StrPrint(s1);
|
||||
i = StrLength(s1);
|
||||
p = (int *)malloc((i + 1) * sizeof(int));
|
||||
get_next(s1, p);
|
||||
printf("NextΪ: ");
|
||||
NextPrint(p, StrLength(s1));
|
||||
printf("\n");
|
||||
|
||||
StrAssign(s1, "abcabx");
|
||||
printf("<EFBFBD>Ӵ<EFBFBD>Ϊ: ");
|
||||
StrPrint(s1);
|
||||
i = StrLength(s1);
|
||||
p = (int *)malloc((i + 1) * sizeof(int));
|
||||
get_next(s1, p);
|
||||
printf("NextΪ: ");
|
||||
NextPrint(p, StrLength(s1));
|
||||
printf("\n");
|
||||
|
||||
StrAssign(s1, "ababaaaba");
|
||||
printf("<EFBFBD>Ӵ<EFBFBD>Ϊ: ");
|
||||
StrPrint(s1);
|
||||
i = StrLength(s1);
|
||||
p = (int *)malloc((i + 1) * sizeof(int));
|
||||
get_next(s1, p);
|
||||
printf("NextΪ: ");
|
||||
NextPrint(p, StrLength(s1));
|
||||
printf("\n");
|
||||
|
||||
StrAssign(s1, "aaaaaaaab");
|
||||
printf("<EFBFBD>Ӵ<EFBFBD>Ϊ: ");
|
||||
StrPrint(s1);
|
||||
i = StrLength(s1);
|
||||
p = (int *)malloc((i + 1) * sizeof(int));
|
||||
get_next(s1, p);
|
||||
printf("NextΪ: ");
|
||||
NextPrint(p, StrLength(s1));
|
||||
printf("\n");
|
||||
|
||||
StrAssign(s1, "ababaaaba");
|
||||
printf(" <20>Ӵ<EFBFBD>Ϊ: ");
|
||||
StrPrint(s1);
|
||||
i = StrLength(s1);
|
||||
p = (int *)malloc((i + 1) * sizeof(int));
|
||||
get_next(s1, p);
|
||||
printf(" NextΪ: ");
|
||||
NextPrint(p, StrLength(s1));
|
||||
get_nextval(s1, p);
|
||||
printf("NextValΪ: ");
|
||||
NextPrint(p, StrLength(s1));
|
||||
printf("\n");
|
||||
|
||||
StrAssign(s1, "aaaaaaaab");
|
||||
printf(" <20>Ӵ<EFBFBD>Ϊ: ");
|
||||
StrPrint(s1);
|
||||
i = StrLength(s1);
|
||||
p = (int *)malloc((i + 1) * sizeof(int));
|
||||
get_next(s1, p);
|
||||
printf(" NextΪ: ");
|
||||
NextPrint(p, StrLength(s1));
|
||||
get_nextval(s1, p);
|
||||
printf("NextValΪ: ");
|
||||
NextPrint(p, StrLength(s1));
|
||||
|
||||
printf("\n");
|
||||
|
||||
StrAssign(s1, "00000000000000000000000000000000000000000000000001");
|
||||
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ: ");
|
||||
StrPrint(s1);
|
||||
StrAssign(s2, "0000000001");
|
||||
printf("<EFBFBD>Ӵ<EFBFBD>Ϊ: ");
|
||||
StrPrint(s2);
|
||||
printf("\n");
|
||||
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӵ<EFBFBD><EFBFBD>ڵ<EFBFBD>%d<><64><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>״<EFBFBD>ƥ<EFBFBD>䣨<EFBFBD><E4A3A8><EFBFBD><EFBFBD>ģʽƥ<CABD><C6A5><EFBFBD>㷨<EFBFBD><E3B7A8>\n", Index(s1, s2, 1));
|
||||
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӵ<EFBFBD><EFBFBD>ڵ<EFBFBD>%d<><64><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>״<EFBFBD>ƥ<EFBFBD>䣨KMP<4D>㷨<EFBFBD><E3B7A8> \n", Index_KMP(s1, s2, 1));
|
||||
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӵ<EFBFBD><EFBFBD>ڵ<EFBFBD>%d<><64><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>״<EFBFBD>ƥ<EFBFBD>䣨KMP<4D><50><EFBFBD><EFBFBD><EFBFBD>㷨<EFBFBD><E3B7A8> \n", Index_KMP1(s1, s2, 1));
|
||||
|
||||
return 0;
|
||||
}
|
||||
150
_03.串/_b.KMP.c
150
_03.串/_b.KMP.c
@@ -2,7 +2,7 @@
|
||||
* @Author: Xu Bai
|
||||
* @Date: 2019-07-08 21:43:47
|
||||
* @LastEditors: Xu Bai
|
||||
* @LastEditTime: 2019-07-08 22:48:27
|
||||
* @LastEditTime: 2019-07-08 22:53:07
|
||||
*/
|
||||
#include "stdio.h"
|
||||
#include "stdlib.h"
|
||||
@@ -133,7 +133,7 @@ int IndexByKMP(String S, String T, int pos)
|
||||
GetNext(T, next);
|
||||
while (i <= S[0] && j <= T[0])
|
||||
{
|
||||
if (j == 0 || S[i] = T[i])
|
||||
if (j == 0 || S[i] == T[i])
|
||||
{
|
||||
++i;
|
||||
++j;
|
||||
@@ -186,7 +186,7 @@ void GetNextVal(String T, int *NextVal)
|
||||
}
|
||||
}
|
||||
|
||||
int IndexByKMP2(String S, String T, int pos)
|
||||
int IndexByKMP1(String S, String T, int pos)
|
||||
{
|
||||
/*i<><69><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>S<EFBFBD><53>ǰλ<C7B0><CEBB><EFBFBD>±<EFBFBD>ֵ */
|
||||
int i = pos;
|
||||
@@ -219,86 +219,86 @@ int IndexByKMP2(String S, String T, int pos)
|
||||
|
||||
int main()
|
||||
{
|
||||
int i, *p;
|
||||
String s1, s2;
|
||||
int i, *p;
|
||||
String s1, s2;
|
||||
|
||||
StrAssign(s1, "abcdex");
|
||||
printf("<EFBFBD>Ӵ<EFBFBD>Ϊ: ");
|
||||
StrPrint(s1);
|
||||
i = StrLength(s1);
|
||||
p = (int *)malloc((i + 1) * sizeof(int));
|
||||
get_next(s1, p);
|
||||
printf("NextΪ: ");
|
||||
NextPrint(p, StrLength(s1));
|
||||
printf("--------------------------------\n");
|
||||
StrAssign(s1, "abcdex");
|
||||
printf("<EFBFBD>Ӵ<EFBFBD>Ϊ: ");
|
||||
StrPrint(s1);
|
||||
i = StrLength(s1);
|
||||
p = (int *)malloc((i + 1) * sizeof(int));
|
||||
GetNext(s1, p);
|
||||
printf("NextΪ: ");
|
||||
NextPrint(p, StrLength(s1));
|
||||
printf("--------------------------------\n");
|
||||
|
||||
StrAssign(s1, "abcabx");
|
||||
printf("<EFBFBD>Ӵ<EFBFBD>Ϊ: ");
|
||||
StrPrint(s1);
|
||||
i = StrLength(s1);
|
||||
p = (int *)malloc((i + 1) * sizeof(int));
|
||||
get_next(s1, p);
|
||||
printf("NextΪ: ");
|
||||
NextPrint(p, StrLength(s1));
|
||||
printf("--------------------------------\n");
|
||||
StrAssign(s1, "abcabx");
|
||||
printf("<EFBFBD>Ӵ<EFBFBD>Ϊ: ");
|
||||
StrPrint(s1);
|
||||
i = StrLength(s1);
|
||||
p = (int *)malloc((i + 1) * sizeof(int));
|
||||
GetNext(s1, p);
|
||||
printf("NextΪ: ");
|
||||
NextPrint(p, StrLength(s1));
|
||||
printf("--------------------------------\n");
|
||||
|
||||
StrAssign(s1, "ababaaaba");
|
||||
printf("<EFBFBD>Ӵ<EFBFBD>Ϊ: ");
|
||||
StrPrint(s1);
|
||||
i = StrLength(s1);
|
||||
p = (int *)malloc((i + 1) * sizeof(int));
|
||||
get_next(s1, p);
|
||||
printf("NextΪ: ");
|
||||
NextPrint(p, StrLength(s1));
|
||||
printf("--------------------------------\n");
|
||||
StrAssign(s1, "ababaaaba");
|
||||
printf("<EFBFBD>Ӵ<EFBFBD>Ϊ: ");
|
||||
StrPrint(s1);
|
||||
i = StrLength(s1);
|
||||
p = (int *)malloc((i + 1) * sizeof(int));
|
||||
GetNext(s1, p);
|
||||
printf("NextΪ: ");
|
||||
NextPrint(p, StrLength(s1));
|
||||
printf("--------------------------------\n");
|
||||
|
||||
StrAssign(s1, "aaaaaaaab");
|
||||
printf("<EFBFBD>Ӵ<EFBFBD>Ϊ: ");
|
||||
StrPrint(s1);
|
||||
i = StrLength(s1);
|
||||
p = (int *)malloc((i + 1) * sizeof(int));
|
||||
get_next(s1, p);
|
||||
printf("NextΪ: ");
|
||||
NextPrint(p, StrLength(s1));
|
||||
printf("--------------------------------\n");
|
||||
StrAssign(s1, "aaaaaaaab");
|
||||
printf("<EFBFBD>Ӵ<EFBFBD>Ϊ: ");
|
||||
StrPrint(s1);
|
||||
i = StrLength(s1);
|
||||
p = (int *)malloc((i + 1) * sizeof(int));
|
||||
GetNext(s1, p);
|
||||
printf("NextΪ: ");
|
||||
NextPrint(p, StrLength(s1));
|
||||
printf("--------------------------------\n");
|
||||
|
||||
StrAssign(s1, "ababaaaba");
|
||||
printf(" <20>Ӵ<EFBFBD>Ϊ: ");
|
||||
StrPrint(s1);
|
||||
i = StrLength(s1);
|
||||
p = (int *)malloc((i + 1) * sizeof(int));
|
||||
get_next(s1, p);
|
||||
printf(" NextΪ: ");
|
||||
NextPrint(p, StrLength(s1));
|
||||
get_nextval(s1, p);
|
||||
printf("NextValΪ: ");
|
||||
NextPrint(p, StrLength(s1));
|
||||
printf("--------------------------------\n");
|
||||
StrAssign(s1, "ababaaaba");
|
||||
printf(" <20>Ӵ<EFBFBD>Ϊ: ");
|
||||
StrPrint(s1);
|
||||
i = StrLength(s1);
|
||||
p = (int *)malloc((i + 1) * sizeof(int));
|
||||
GetNext(s1, p);
|
||||
printf(" NextΪ: ");
|
||||
NextPrint(p, StrLength(s1));
|
||||
GetNextVal(s1, p);
|
||||
printf("NextValΪ: ");
|
||||
NextPrint(p, StrLength(s1));
|
||||
printf("--------------------------------\n");
|
||||
|
||||
StrAssign(s1, "aaaaaaaab");
|
||||
printf(" <20>Ӵ<EFBFBD>Ϊ: ");
|
||||
StrPrint(s1);
|
||||
i = StrLength(s1);
|
||||
p = (int *)malloc((i + 1) * sizeof(int));
|
||||
get_next(s1, p);
|
||||
printf(" NextΪ: ");
|
||||
NextPrint(p, StrLength(s1));
|
||||
get_nextval(s1, p);
|
||||
printf("NextValΪ: ");
|
||||
NextPrint(p, StrLength(s1));
|
||||
StrAssign(s1, "aaaaaaaab");
|
||||
printf(" <20>Ӵ<EFBFBD>Ϊ: ");
|
||||
StrPrint(s1);
|
||||
i = StrLength(s1);
|
||||
p = (int *)malloc((i + 1) * sizeof(int));
|
||||
GetNext(s1, p);
|
||||
printf(" NextΪ: ");
|
||||
NextPrint(p, StrLength(s1));
|
||||
GetNextVal(s1, p);
|
||||
printf("NextValΪ: ");
|
||||
NextPrint(p, StrLength(s1));
|
||||
|
||||
printf("--------------------------------\n");
|
||||
printf("--------------------------------\n");
|
||||
|
||||
StrAssign(s1, "00000000000000000000000000000000000000000000000001");
|
||||
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ: ");
|
||||
StrPrint(s1);
|
||||
StrAssign(s2, "0000000001");
|
||||
printf("<EFBFBD>Ӵ<EFBFBD>Ϊ: ");
|
||||
StrPrint(s2);
|
||||
printf("--------------------------------\n");
|
||||
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӵ<EFBFBD><EFBFBD>ڵ<EFBFBD>%d<><64><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>״<EFBFBD>ƥ<EFBFBD>䣨<EFBFBD><E4A3A8><EFBFBD><EFBFBD>ģʽƥ<CABD><C6A5><EFBFBD>㷨<EFBFBD><E3B7A8>\n", Index(s1, s2, 1));
|
||||
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӵ<EFBFBD><EFBFBD>ڵ<EFBFBD>%d<><64><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>״<EFBFBD>ƥ<EFBFBD>䣨KMP<4D>㷨<EFBFBD><E3B7A8> \n", Index_KMP(s1, s2, 1));
|
||||
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӵ<EFBFBD><EFBFBD>ڵ<EFBFBD>%d<><64><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>״<EFBFBD>ƥ<EFBFBD>䣨KMP<4D><50><EFBFBD><EFBFBD><EFBFBD>㷨<EFBFBD><E3B7A8> \n", Index_KMP1(s1, s2, 1));
|
||||
StrAssign(s1, "00000000000000000000000000000000000000000000000001");
|
||||
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ: ");
|
||||
StrPrint(s1);
|
||||
StrAssign(s2, "0000000001");
|
||||
printf("<EFBFBD>Ӵ<EFBFBD>Ϊ: ");
|
||||
StrPrint(s2);
|
||||
printf("--------------------------------\n");
|
||||
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӵ<EFBFBD><EFBFBD>ڵ<EFBFBD>%d<><64><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>״<EFBFBD>ƥ<EFBFBD>䣨<EFBFBD><E4A3A8><EFBFBD><EFBFBD>ģʽƥ<CABD><C6A5><EFBFBD>㷨<EFBFBD><E3B7A8>\n", Index(s1, s2, 1));
|
||||
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӵ<EFBFBD><EFBFBD>ڵ<EFBFBD>%d<><64><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>״<EFBFBD>ƥ<EFBFBD>䣨KMP<4D>㷨<EFBFBD><E3B7A8> \n", IndexByKMP(s1, s2, 1));
|
||||
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӵ<EFBFBD><EFBFBD>ڵ<EFBFBD>%d<><64><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>״<EFBFBD>ƥ<EFBFBD>䣨KMP<4D><50><EFBFBD><EFBFBD><EFBFBD>㷨<EFBFBD><E3B7A8> \n", IndexByKMP1(s1, s2, 1));
|
||||
|
||||
getchar();
|
||||
return OK;
|
||||
|
||||
Reference in New Issue
Block a user