楼主是来骗作业的 code里面什么都没有
第二题 如果是0-99个100个连续的数字里存在相同的两个数并少一个数 还可以做做 1-99那面试官脑袋进水了
鄙视下
第二题 如果是0-99个100个连续的数字里存在相同的两个数并少一个数 还可以做做 1-99那面试官脑袋进水了
鄙视下
程序代码:#include<iostream>
#include<cstring>
#include<climits>
#define LEN(x) ((strlen(x))-0)
using namespace std;
class LCS{
private:
char * str1;
char * str2;
int **c;
int **b;
protected:
void Print(int i,int j)
{
if( i==0 || j == 0)
return;
if( b[i][j]== 0)
{
Print(i-1,j-1);
cout<<str1[i];
}
else if( b[i][j] == -1)
Print(i,j-1);
else
Print(i-1,j);
}
public:
LCS(){
str1=" adbccadebbca";
str2=" edabccadece";
c=new int * [LEN(str1)+1];
b=new int * [LEN(str1)+1];
for(int i=0;i<LEN(str1)+1;i++)
{
c[i]=new int[LEN(str2)+1];
b[i]=new int[LEN(str2)+1];
}
}
~LCS()
{
for(int i=0;i<LEN(str1)+1;i++)
{
delete [] c[i];
delete [] b[i];
}
delete [] c;
delete [] b;
}
int LCS_Lenth()
{
for(int i=0;i<=LEN(str1);i++)
{
c[i][0]=0;
}
for(int j=0;j<=LEN(str2);j++)
{
c[0][j]=0;
}
for(int i=1;i<=LEN(str1);i++)
{
for(int j=1;j<LEN(str2);j++)
c[i][j]=INT_MIN;
}
for(int i=1;i<LEN(str1);i++)
{
for(int j=1;j<LEN(str2);j++)
{
if( str1[i]==str2[j])
{
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][j-1];
b[i][j]=-1;
}
else
{
c[i][j]=c[i-1][j];
b[i][j]=1;
}
}
}
return c[LEN(str1)-1][LEN(str2)-1];
}
void LCS_Path(){
Print(LEN(str1)-1,LEN(str2)-1);
cout<<endl;
}
};
int main()
{
LCS lcs;
cout<<lcs.LCS_Lenth()<<endl;
lcs.LCS_Path();
return 0;
}