| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 811 人关注过本帖
标题:写kmp有个疑问
只看楼主 加入收藏
企鹅
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2006-7-14
收藏
 问题点数:0 回复次数:3 
写kmp有个疑问

#include <iostream>
using namespace std;

void get_next(char *c,int *i);
int Index_KMP(char* s,char* T,int pos);

int main()
{
char c[100],d[100];
int b[100];
scanf("%s %s",c,d);
int i,j;
i=Index_KMP(c,d,1);
printf("i=%d\n",i);
return 0;
}

void get_next(char *c,int *next)
{
int i,len,j;
next[0]=-1;
i=0;
j=-1;
len=strlen(c);
while(i<len-1)
{
if(j==-1||c[i]==c[j])
{
i++;
j++;
next[i]=j;
}
else j=next[j];
}
}

int Index_KMP(char* s,char* T,int pos)
{
int j=0,i=0,*p;
p=new int[strlen(T)];
get_next(T,p);
j=0;
while((j!=strlen(T))&&(i!=strlen(s)))
//while((j<strlen(T))&&(i<strlen(s)))
{
if(j==-1)
{
i++;
j++;
}
else if(T[j]==s[i])
{
i++;
j++;
}
else
j=p[j];
}
if(j==strlen(T))
return i-strlen(T)+1;
return -1;
}

为什么用第二个while 会发生错误啊 希望有人可以看看

搜索更多相关主题的帖子: kmp 疑问 
2007-10-12 19:08
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 

/*---------------------------------------------------------------------------
File name: kmp-2.cpp
Author: HJin (email: fish_sea_bird [at] yahoo [dot] com )
Created on: 10/13/2007 18:54:23
Environment: WinXPSP2 En Pro + VS2005 v8.0.50727.762

10/13/2007 20:13:17
-----------------------------------------------------------
0. modified the first implementation so that the current
presentation of algorithms

Index() and Index_KMP()

excatly immitates what the textbook said.

1. added boundary checking. Now this piece of code can
pass rigorous testing.

-----------------------------------------------------------
All indices start from 0, not 1.

O(n, m) means O(n) time, O(m) space.
O(n) means O(n) time, O(1) space.

This implementation does not check boundary conditions, such as
t=NULL, strlen(t)=0, etc.
*/

#include <iostream>
using namespace std;

/**
Algorithm 4.5 (Yan, Weimin, P 79)

O(n*m).
*/
int Index(char* s, char* t, int pos)
{
int i=pos, j=0, m, n;

/**
Check if s or t are NULL pointers. The reason:

you cannot apply strlen() to a NULL pointer.
*/
if(s==NULL)
{
return -1;
}
/**
s != NULL after this point.
*/

/**
We treat t=NULL as an empty string and return 0.
*/
if(t==NULL)
{
return 0;
}
/**
t != NULL after this point.

both s and t are not NULL after this point.
*/

/**
We can have a char pointer which has length 0
but not NULL:

char s[]="";
*/
n = strlen(s);
if(pos<0 || pos>=n || n==0)
{
return -1;
}
m = strlen(t);
if(m==0)
{
return 0;
}
if(m>n)
{
return -1;
}
/**
Boundary checking is done at this point. We have

s!=NULL, t!=NULL, n>=m>0, and 0<=pos<=n-1;
*/

while(i<n && j<m)
{
if(s[i] == t[j])
{
++i;
++j;
}
else
{
i += 1-j;
j = 0;
}
}

if(j == m)
{
return i-m;
}
else
{
return -1;
}
}

/** Compute the next[] table.
Algorithm 4.8 (Yan, Weimin, P 84)

O(m, m).
*/
void get_nextval(char* t, int*& nextval)
{
/**
boundary checking for t is done in Index_KMP().
*/
int m=strlen(t)-1, j=-1, i=0;

nextval[0]=-1;
while(i<m)
{
if(j==-1 || t[i]==t[j])
{
++i;
++j;
if(t[i]==t[j])
{
nextval[i] = nextval[j];
}
else
{
nextval[i] = j;
}
}
else
{
j=nextval[j];
}
}
}

/** Implement the KMP string matching algorithm.

Algorithm 4.6 (Yan, Weimin, P 82)

O(n+m, m).
*/
int Index_KMP(char* s, char* t, int pos)
{
int m, n, i=pos, j=0, *nextval=NULL;

/**
Check if s or t are NULL pointers. The reason:

you cannot apply strlen() to a NULL pointer.
*/
if(s==NULL)
{
return -1;
}
/**
s != NULL after this point.
*/

/**
We treat t=NULL as an empty string and return 0.
*/
if(t==NULL)
{
return 0;
}
/**
t != NULL after this point.

both s and t are not NULL after this point.
*/

/**
We can have a char pointer which has length 0
but not NULL:

char s[]="";
*/
n = strlen(s);
if(pos<0 || pos>=n || n==0)
{
return -1;
}
m = strlen(t);
if(m==0)
{
return 0;
}
if(m>n)
{
return -1;
}
/**
Boundary checking is done at this point. We have

s!=NULL, t!=NULL, n>=m>0, and 0<=pos<=n-1;
*/

nextval = new int[m];
get_nextval(t, nextval);

while(i<n && j<m)
{
if(j==-1 || s[i]==t[j])
{
++i;
++j;
}
else
{
j=nextval[j];
}
}

delete [] nextval;

if(j==m)
{
return i-m;
}
else
{
return -1;
}
}

int main()
{
char s[] = "abcdefgh";
char t[] = "cd";
//char t[] = "";
//char *t = NULL;

int i=-1, pos;
pos=-1;

i= Index(s, t, pos);
if(i!=-1)
{
cout<<s+i<<endl;
}
else
{
cout<<t<<" is not a substring of "<<s<<" starting at position "<<pos<<endl;
}

i=Index_KMP(s, t, pos);
if(i!=-1)
{
cout<<s+i<<endl;
}
else
{
cout<<t<<" is not a substring of "<<s<<" starting at position "<<pos<<endl;
}

return 0;
}


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-10-14 21:08
discus815
Rank: 1
等 级:新手上路
帖 子:43
专家分:0
注 册:2007-10-11
收藏
得分:0 

It's very good!


旋转的木马,没有翅膀,但是却可以带着你飞翔.......
2007-10-15 12:11
zmfttkl
Rank: 1
等 级:新手上路
帖 子:148
专家分:0
注 册:2007-7-1
收藏
得分:0 
支持!顶!!

2007-10-15 12:44
快速回复:写kmp有个疑问
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.021937 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved