#include<iostream>
using namespace std;
#define maxlen 255
#define chunksize 80
typedef struct
{
char ch[maxlen + 1];//ch[0]不存字符
int length;
}sstring;//串的顺序存储结构(常用)
typedef struct chunk //串的链式存储
{
char ch[chunksize];
struct chunk* next;
}chunk;
typedef struct
{
chunk* head, * tail;//串的头指针和尾指针
int curlen;//串的当前长度
}Lstring;//字符串的块链结构
//BF算法
int index_bf(sstring s, sstring t,int pos)//从pos处开始找
{
int i = pos, j = 1;
while (i <= s.length && j <= t.length)
{
if (s.ch[i] != t.ch[j])
{
i++;
j++;
}
else
{
i = i - j + 2;
j = 1;
}
}
if (j > t.length)
{
return i - t.length;
}
else
{
return 0;//(即失败)
}
}
//KMP算法
void get_next(sstring t, int*& next)
{
int i = 1;
next[1] = 0;
int j = 0;
while (i < t.length)
{
if (j == 0 || t.ch[i] == t.ch[j])
{
i++;j++;
next[i] = j;
}
else
{
j = next[j];
}
}
}
void get_nextval(sstring t, int*& nextval)
{
int i = 1;
nextval[1] = 0;
int j = 0;
while (i < t.length)
{
if (j == 0 || t.ch[i] == t.ch[j])
{
i++;
j++;
if (t.ch[i] != t.ch[j])
{
nextval[i] = j;
}
else
{
nextval[i] = nextval[j];
}
}
else
{
j = nextval[j];
}
}
}
int index_kmp(sstring s, sstring t, int pos)//从pos处开始找
{
int i = pos, j = 1;
int* next = new int[t.length];
get_next(t, next);
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;//(即失败)
}
}
int main()
{
return 0;
}