LCS算法的一个小问题

yulinlover 2008-07-31 10:02:51
为什么我用DEBUG版本没有问题,但是用Release版本程序就会崩溃?
以下是我在VB里的调用原形:
Public Declare Function GetLcsString Lib "Algorithm.dll" (ByVal lpItem1 As String, ByVal lpItem2 As String) As String

下面给出C++的LCS算法原码:

// Algorithm.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}

char* GetLcsItem(int **b,char* lpItem1,int i, int j)
{
int x,y,z,w;
int iIndex;
char chrTemp;
char* lpRetItem;

lpRetItem=new char[i>j?j:i];
memset(lpRetItem,0,i>j?j:i);

iIndex=0;
lpRetItem = new char[i>j?j:i];

z=j;
w=i;
for(x=w;x>=0;x--)
{
for(y=z;y>=0;y--)
{
if(b[x][y]==0)
{
lpRetItem[iIndex++]=lpItem1[x-1];
w=x-1;
z=y-1;
break;
}
else if(b[x][y]==-1)
{
w=x-1;
z=y;
break;
}
else
{
continue;
}
}
}

lpRetItem[iIndex-1]='\0';
y = strlen(lpRetItem);
for(x=0;x<y/2;x++)
{
chrTemp = lpRetItem[x];
lpRetItem[x] =lpRetItem[y-x-1];
lpRetItem[y-x-1]=chrTemp;
}
return lpRetItem;
}

char* _stdcall GetLcsString(char* lpItem1,char* lpItem2)
{
int **c;
int **b;
int i,j;
int m,n;
char* lpRetItem; //获取两字符串的长度
m = strlen(lpItem1);
n = strlen(lpItem2);//初始化二维空间
b = new int*[m+1];
c = new int*[m+1];
for(i=0;i<m+1;i++)
{
b[i]=new int[n+1];
c[i]=new int[n+1];
}
//初始化二维空间的第一行和第一列空0值;
for(i=0;i<m+1;i++)
{
for(j=0;j<n+1;j++)
{
b[i][j]=0;
}
c[i][0]=0;
}
for(i=0;i<n+1;i++)
{
b[0][i]=0;
c[0][i]=0;
}

for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
if(lpItem1[i-1]==lpItem2[j-1])
{
c[i][j]=c[i-1][j-1]+1;
b[i][j]=0;
}
else if(c[i-1][j]>=c[i][j-1])
{
c[i][j]=c[i-1][j];//当前最长公共子序列可以不需要x[i-1]
b[i][j]=-1;
}
//和上面分析类似
else
{
c[i][j]=c[i][j-1];//当前最长公共子序列可以不需要y[j-1]
b[i][j]=1;
}
}
}


//PrintArray(c,m+1,n+1);
//PrintArray(b,m+1,n+1);

lpRetItem=GetLcsItem(b,lpItem1,m,n);
for(i=0;i<m+1;i++)
{
delete c[i];
delete b[i];
c[i]=NULL;
b[i]=NULL;
}
delete []c;
delete []b;
c=NULL;
b=NULL;
return lpRetItem;
}

...全文
109 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yulinlover 2008-08-04
  • 打赏
  • 举报
回复
没有人能改啊?出来帮忙一下啊
yulinlover 2008-08-01
  • 打赏
  • 举报
回复
C++的版块也太冷了吧,所以到此求救,请各位达人不吝赐教!

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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