void LCS(char *const x,char *const y)
{//求最长的公共子序列 衡量字符串相似度一种方法
try
{
int lenX=strlen(x);
int lenY=strlen(y);
int **P;
CreateTwoNdims<int>(P,lenX+1,lenY+1);
for(int i=0;i<=lenY;++i)
{
P[0][i]=0;
}
for(int i=0;i<=lenX;++i)
{
P[i][0]=0;
}
for(int i=1;i<=lenX;++i)
{
for(int j=1;j<=lenY;++j)
{
if(x[i-1]==y[j-1])
P[i][j]=P[i-1][j-1]+1;
else
P[i][j]=max(P[i-1][j],P[i][j-1]);
}
}
std::cout<<P[lenX][lenY]<<std::endl;
DestroyTwoNdims(P,lenX+1,lenY+1);
}
catch(std::exception err)
{
std::cout<<err.what()<<std::endl;
}
}
void ComputeStringDis(char *const x,int lowX,int highX,char *const y,int lowY,int highY,int &dis)
{//编辑字符串距离 另一种字符串相似度度量方法
if(lowX<=highX&&lowY<=highY)
{
if(x[lowX]==y[lowY])
{
int Dis=0;
ComputeStringDis(x,lowX+1,highX,y,lowY+1,highY,Dis);
dis+=1;
}
else
{
int lDis=0,rDis=0;
ComputeStringDis(x,lowX+1,highX,y,lowY,highY,lDis);
ComputeStringDis(x,lowX,highX,y,lowY+1,highY,rDis);
dis+=1;
dis+=min(lDis,rDis);
}
}
else
{
dis+=highX>=lowX?highX-lowX+1:0;
dis+=highY>=lowY?highY-lowY+1:0;
}
}
void RunAlgorithm()
{
char *xx=”abdd”;
char *yy=”aebdd”;
int dis=0;
ComputeStringDis(xx,0,3,yy,0,4,dis);
std::cout<<dis<<std::endl;
char *x=”abcbdab”;
char *y=”bdcaba”;
LCS(x,y);
}