菜鸟求助

DontKissBossAss 2010-01-20 11:32:42
求个C\C++语言版本的KMP 算法,带Main 函数那种

我这几天一直在纠结这个问题,一直不明白,或许看深了就浮躁的原因吧。那理论有4页,看到一半就开始浮躁了。 勉强看完也因为大闹栈内存不足,崩溃了。。。。

于是就google 或者构造严蔚敏数上边的代码,我构造一直失败。然后只能去google了,找了N多个,均失败。

无奈的发现了,国人直接copy的也太多了。

无奈只能发帖了。只要是能通过,运行OK的就行
下边代码是因为不懂的KMP道理引起的,请不要追究,Next没有初始化。
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;
}
...全文
141 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
DontKissBossAss 2010-01-20
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 traceless 的回复:]
C/C++ code#include<wchar.h>
#include<iostream>
#include<string>usingnamespace std;void KmpSlip(wchar_t*pwszDes,int m,int*pNext)//生成“滑动函数”{int i,j;
pNext[0]=-1;for(j=1; j< m; j++)
{
i=?-
[/Quote]

输入 1: 000000000001
输入2: 0001
结果: No Found!
DontKissBossAss 2010-01-20
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 traceless 的回复:]
嘿嘿 来蹭分的
[/Quote]

嘿嘿,刚吃饭回来,我先事实,好就给你,不过,老大,这是算法区,积分MS不积累啊
traceless 2010-01-20
  • 打赏
  • 举报
回复
嘿嘿 来蹭分的
traceless 2010-01-20
  • 打赏
  • 举报
回复
#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;
}
DontKissBossAss 2010-01-20
  • 打赏
  • 举报
回复
请无视我代码吧,也不想解释了。对点代码,真正做了什么,我很清楚。

不清楚kpm,也没动力改。
versaariel 2010-01-20
  • 打赏
  • 举报
回复
j = tmp[j];
VS2005 DEBUG了下,并WATCH,赋值部分出现了0xcdcdcdcd
发现int* tmp = new int[arr_Len];可能是空间开了,但是指针没初始化。
我对new操作不熟悉,你再看看
qq497525725 2010-01-20
  • 打赏
  • 举报
回复
不太懂 顶下。
xiang2011 2010-01-20
  • 打赏
  • 举报
回复
又是模式匹配,让人头疼的算法啊!!
traceless 2010-01-20
  • 打赏
  • 举报
回复
更正一下:

if (j == nDes)
{
nIndex = i-nDes+2;
break;
}
traceless 2010-01-20
  • 打赏
  • 举报
回复
#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;
}


这个可以了,我修改变成更强大的处理了。。。
hityct1 2010-01-20
  • 打赏
  • 举报
回复
mark
versaariel 2010-01-20
  • 打赏
  • 举报
回复
昨天刚看过KMP算法,还是学习一下
lzy18lzy 2010-01-20
  • 打赏
  • 举报
回复
我贴个代码吧!


#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;
}

33,027

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧