源程序如下:
#include <iostream.h>
#include <string.h>
#define N 100
char a[N],b[N],str[N];
int lcs_len(char *a, char *b, int c[N][N])
{
int m=strlen(a), n=strlen(b), i,j;
for (i=0;i<=m;i++) c[i][0]=0;
for (i=0;i<=n;i++) c[0][i]=0;
for (i=1;i<=m;i++)
for (j=1;j<=m;j++)
if (a[i-1]==b[j-1])
c[i][j]=c[i-1][j-1]+1;
else if (c[i-1][j]>=c[i][j-1])
c[i][j]=c[i-1][j];
else
c[i][j]=c[i][j-1];
return c[m][n];
}
char *buile_lcs(char s[],char *a, char *b)
{
int k, i=strlen(a), j=strlen(b), int c[N][N];
k=lcs_len(a,b,c);
s[k]='\0';
while (k>0)
if (c[i][j]==c[i-1][j]) i--;
else if (c[i][j]==c[i][j-1]) j--;
else
{
s[--k]=a[i-1];
i--; j--;
}
return s;
}
void main()
{
char *p1,*p2;
p1=a;p2=b;
cout<<"Enter two string <"<<N <<"!"<<endl;
cin>>a>>b;
cout<<buile_lcs(str,p1,p2)<<endl;
}
在Microsoft Visual Studio 6.0环境下能通过编译,但是存在警告
--------------------Configuration: chj_5 - Win32 Debug--------------------
Compiling...
5.cpp
E:\Program Files\Microsoft Visual Studio\MyProjects\chj_5\5.cpp(24) : warning C4518: 'int ' : storage-class or type specifier(s) unexpected here; ignored
E:\Program Files\Microsoft Visual Studio\MyProjects\chj_5\5.cpp(24) : warning C4228: nonstandard extension used : qualifiers after comma in declarator list are ignored
Linking...
chj_5.exe - 0 error(s), 2 warning(s)
继续运行,在数组长度较短的情况下可以正常得出结果,但是数组长度较大时就出现如下情况:
各位达人帮帮忙````指导指导``不胜感激!!
[此贴子已经被作者于2005-12-28 17:51:22编辑过]