33,027
社区成员




int* _Get_Next (const char* p)
{
if (p == NULL)
return NULL;
int arr_Len = strlen (p);
int* tmp = new int[arr_Len];
int i = 1, j = 0;
*tmp = 0;
//get the
while (i < arr_Len)
{
if ( (j == 0) || (p[j] == p [i]) )
{
i++, j++;
tmp[i] = j;
}
else
//issue here,
j = tmp[j];
}
return tmp;
}
#include <wchar.h>
#include <iostream>
#include <string>
using namespace std;
void KmpSlip(wchar_t *pwszDes,int m,int *pNext) //生成“滑动函数”
{
int i,j;
pNext[0] = -1;
for(j=1; j < m; j++)
{
i = pNext[j-1];
while((i >=0 ) && (pwszDes[i] != pwszDes[j-1]))
i = pNext[i];
pNext[j] = i+1;
}
}
int KmpFind(wchar_t *pwszSrc, int nSrc, wchar_t *pwszDes, int nDes, int next[]) //进行匹配
{
int i=0,j=0;
while(i <= nSrc - nDes)
{
while((j >= 0) && (pwszDes[j]!= pwszSrc[i]))
j=next[j];
if(j == nDes-1)
{
return i+2-nDes;
}
else
{
i++;
j++;
}
}
return -1;
}
int main(void)
{
wchar_t wszSrc[21], wszDes[6];
int next[6];
int m,n;
wprintf(L" 6 <= size <= 20:");
wscanf(L"%s", wszSrc);
wprintf(L"\ni size <= 5:");
wscanf(L"%s", wszDes);
KmpSlip(wszDes, wcslen(wszDes), next);
int nIndex = KmpFind(wszSrc, wcslen(wszSrc), wszDes, wcslen(wszDes), next);
if (-1 == nIndex)
wcout<<L"no find!"<<endl;
else
{
wcout<<L"src:"<<wszSrc<<endl;
wcout<<L"dec:"<<wszDes<<endl;
wcout<<L"resuld index:"<<nIndex<<endl;
}
return 0;
}
if (j == nDes)
{
nIndex = i-nDes+2;
break;
}
#include <wchar.h>
#include <iostream>
#include <string>
#include <locale.h>
using namespace std;
void KmpSlip(const wchar_t *pwszDes, int nDes, int *pNext) //生成“滑动函数”
{
int i = 0;
for(int j = 1; j < nDes; j++)
{
while((i > 0 ) && (pwszDes[i] != pwszDes[j]))
i = pNext[i - 1];
if (pwszDes[i] == pwszDes[j])
i++;
pNext[j] = i;
}
}
int KmpMatch(const wchar_t *pwszSrc, int nSrc, const wchar_t *pwszDes, int nDes) //进行匹配
{
int *pNext = new int[nDes];
memset(pNext, 0, nDes*sizeof(int));
KmpSlip(pwszDes, nDes, pNext);
int i=0, j=0, nIndex = -1;
while(i++ < nSrc)
{
while((j > 0) && (pwszDes[j] != pwszSrc[i]))
j = pNext[j-1];
if(pwszDes[j] == pwszSrc[i])
j++;
if (j == nDes)
nIndex = i-nDes+2;
}
delete[] pNext;
return nIndex;
}
int main(void)
{
setlocale(LC_ALL,"chs");
wstring wstrSrc;
wstring wstrDec;
wcout << L"输入源字符串: ";
wcin >> wstrSrc;
wcout << L"输入匹配字符串: ";
wcin >> wstrDec;
int nIndex = KmpMatch(wstrSrc.c_str(), wstrSrc.size(), wstrDec.c_str(), wstrDec.size());
if (-1 == nIndex)
wcout<<L"no find!"<<endl;
else
{
wcout<<L"src:"<<wstrSrc<<endl;
wcout<<L"dec:"<<wstrDec<<endl;
wcout<<L"resuld index:"<<nIndex<<endl;
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX_LEN 1024
int result;
char str1[MAX_LEN];
char str2[MAX_LEN];
int the_next[MAX_LEN];
//建立 next
void find_next(char *str,int len,int *next)
{
int i,j;
next[0] = -1;
i = -1; j = 0;
while(j<len)
{
if(i == -1 || str[i] == str[j])
{
i++;
j++;
next[j] = i;
}
else i = next[i];
}
}
//查找串
int find_strs(char *st1,char *st2,int len1,int len2,int *next)
{
int i,j,result;
i = 0;
j = 0;
result = -1;
while(j<len1)
{
if(i == -1 || str1[j] == str2[i])
{
i++;
j++;
if(i == len2)
{
result = j-len2;
break;
}
}
else
i = next[i];
}
return result;
}
int main()
{
int test;
while(scanf("%s%s",str1,str2)!=EOF)
{
find_next(str2,strlen(str2),the_next);
test = find_strs(str1,str2,strlen(str1),strlen(str2),the_next);
if(test == -1)
printf("no find!\n");
else
printf("start: %d\n",test+1);
}
return 0;
}