110,536
社区成员
发帖
与我相关
我的任务
分享
// 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;
}