BF/KMF char储存十六进制
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include<fstream>
#include <stack>
#include <assert.h>
using namespace std;
char s[]=" ababcabcacbab";
char t[]=" abcac";
int BF (char s[],char t[])
{
//assert(s[0]);
//assert(t[0]);
int i=1;
int j=1;
while (i<=s[0]&&j<=t[0])
{
if(s[i]==t[j])
{
i++;
j++;
}
else
{
i=i-j+2;
j=1;
}
}
if(j>t[0])
return i-j+1;
else return 0;
}
void GetNext(char T[ ], int next[ ])
{ int j=0, k=-1;
next[0]=-1;
while(j<strlen(T)-1)
{
if (k==-1||T[j]==T[k])
{
j++;
k++;
next[j]=k;
}
else k=next[k];
}
}
int KMP(char S[],char T[])
{
int next[8],i=0,j=0,k=-1;
GetNext(T,next);
while (i<(int)strlen(S)&&j<(int)strlen (T))
{
if (j==-1||S[i]==T[j])
{
i++;j++;
}
else j=next[j];
}
for ( i=0;i<5;i++)
{
cout<<next[i];
}
if (j>=strlen(T))
return i-strlen(T);
else
return -1;
}
int main() {
s[0]=0x10;
t[0]=0x05;
char *S="ababcabaabcaabaababcaab",*T="abaababc";
int i,next[Maxsize];
cout << BF(s,t) << endl;
return 0;
}