(min (m, n))空间求出最长公共子序列的长度.这个问题琢磨了好久,终于还是睾出来了. 实现的思想就是,从两个串中选出一个较长的串.用另外一个串的所有,同较长串的当前值比较.只记录最大的长度,最终完成. 说的,比较简单.研究过程,还算一般.全下来大约4小时.是慢还是快?貌似很慢啊,希望自己今后养成高速解决问题的习惯吧. 还有啊,网吧是在是太吵了...
int maxLenth (const char * const strA, const char * const strB) ;
int _tmain(int argc, _TCHAR* argv[])
{
char * strA = " ABCDBABCBABDA", * strB = " ABDBAABABCBDBABCDABA" ;
std ::cout << maxLenth(strA, strB) << std ::endl ;
return 0 ;
}
// Assume aSize <= bSize
int maxLenth (const char * const strA, const char * const strB)
{
int aSize = strlen(strA) ;
int bSize = strlen(strB) ;
int * maxLenth = new int[aSize] ;
if (!maxLenth)
return -1 ;
for (int i = 0; i < aSize; ++i)
maxLenth[i] = 0 ;
for (int i = 0; i < bSize; ++i)
{
if (strB[i] == strA[0])
maxLenth[0] = 1 ;
else
maxLenth[0] = 0 ;
for (int j = 1; j < aSize; ++j)
{
if (strB[i] == strA[j])
{
int max = 0 ;
for (int k = j - 1; k >= 0; --k)
{
if (maxLenth[k] < i + 1 && maxLenth[k] > max)
max = maxLenth[k] ;
}
if (max == maxLenth[j])
maxLenth[j] = maxLenth[j] + 1 ;
else if (max + 1 < maxLenth[j])
continue ;
else maxLenth[j] = max + 1 ;
}
}
}
int max = maxLenth[aSize - 1] ;
delete []maxLenth ;
return max ;
}