我试图解决最常见的子序列问题。我面临整数数组到指针转换的问题。我的2D数组的最后一个元素将是结果。这是我的代码:
#include<iostream>
#include<cstring>
using namespace std;
char firstSequence_X[999999],secondSequence_Y[999999];
int length_M,length_N;
void LCS_LENGTH(char*,char*);
int main(void)
{
LCS_LENGTH(firstSequence_X,secondSequence_Y);
return 0;
}
void LCS_LENGTH(char X[],char Y[])
{
//scan
cout<<"Enter first sequence : ";
cin>>X;
length_M=strlen(X);
cout<<"Enter second sequence : ";
cin>>Y;
length_N=strlen(Y);
cout<<endl;
cout<<"First sequence's length is : "<<length_M<<endl;
cout<<"Second sequence's length is : "<<length_N<<endl;
//declare 2 matrix : C for length , B for sign
int C[length_M+1][length_N+1],B[length_M+1][length_N+1];
//loop variable
int i,j;
//clear all
for(i=0;i<=length_M;i++)
{
C[i][0]=0;
B[i][0]='n';
}
for(j=0;j<=length_N;j++)
{
C[0][j]=0;
B[0][j]='n';
}
//dp
for(i=1;i<=length_M;i++)
{
for(j=1;j<=length_N;j++)
{
if(X[i]==Y[j])
{
C[i][j]=C[i-1,j-1]+1;//diagonal+1
B[i][j]='d';
}
else if(C[i-1][j]>=C[i][j-1])
{
C[i][j]=C[i-1,j];//up row copy
B[i][j]='u';
}
else
{
C[i][j]=C[i-1,j];//left column copy
B[i][j]='l';
}
}
}
cout<<endl<<"C[i,j] :"<<endl;
for(i=0;i<=length_M;i++)
{
for(j=0;j<=length_N;j++)
{
cout<<C[i][j]<<" ";
}
cout<<endl;
}
cout<<endl<<"B[i,j] :"<<endl;
for(i=0;i<=length_M;i++)
{
for(j=0;j<=length_N;j++)
{
cout<<B[i][j]<<" ";
}
cout<<endl;
}
}
我必须打印B和C数组。但是显示了以下错误消息:从int *到int的无效转换。请纠正我的错误。 TIA
答案 0 :(得分:3)
看起来你做错了以下事情。
WebUtilLogging=on
WebUtilLoggingDetail=normal
我认为这应该是
C[i][j]=C[i-1,j-1]+1;//diagonal+1
和
C[i][j]=C[i-1][j-1]+1;//diagonal+1
应该是
C[i][j]=C[i-1,j];//up row copy
和
C[i][j]=C[i-1][j];//up row copy
应该是
C[i][j]=C[i-1,j];//left column copy