g***j 发帖数: 1275 | 1 哪个大侠帮我看看下面这两个edit distance的code,三个操作,add, delete, and
replacement.
为啥第二个code用test case测试的时候总是错的?
int editDistance(char a[], int sizea, char b[], int sizeb) {
if(sizea < 0 || sizeb < 0 || a == NULL || b == NULL ) return -1;
if(sizea == 0 ) return sizeb;
if(sizeb == 0 ) return sizea;
if(a[sizea-1] == b[sizeb -1])
return editDistance(a, sizea - 1, b, sizeb - 1);
else
return min(min(editDistance(a, sizea, b, sizeb - 1) + 1, //delete
... 阅读全帖 |
|
a********n 发帖数: 1287 | 2 用suffix array
char* LongestCommonSubStr( char* a, char* b )
{
if( !a || !b )
{
return NULL;
}
int sizea = strlen( a );
int sizeb = strlen( b );
char** suffix = new char*[sizea + sizeb];
int suffixIdx = 0;
for( int i = 0; i < sizea; i++ )
{
suffix[suffixIdx++] = a + i;
}
for( int i = 0; i < sizeb; i++ )
{
suffix[suffixIdx++] = b + i;
}
std::sort( suffix, suffix + sizea + sizeb, Comp() );
int maxLen = 0;
char* ... 阅读全帖 |
|
i********s 发帖数: 133 | 3 the code for the 1st problem. assume A[0] <= B[0].
int findK(int *A, int *B,
unsigned int sizeA, unsigned int sizeB,
unsigned k)
{
if (sizeA == 0)
{
if (k < sizeB)
return B[k-1];
else
throw std::exception();
}
if (sizeB == 0)
{
if (k
return A[k-1];
else
throw std::exception();
}
unsigned int index1, index2;
index1 = std::min(k-1, sizeA-1);
index2 = 0;
|
|
u*****o 发帖数: 1224 | 4 你们BS我吧,我又问弱智问题了。。
我用NEW来初始化2D array的时候都是这么写的。。
int** a = new int*[sizeA];
For (i=0;i
a[i] = new int[sizeB];}
但今天这么写的也WORK了
int a[,] = new int[sizeA][sizeB];
看到别人这么写过也行。。
int* a = new int[sizeA, sizeB];
想问问大家有区别吗。。我觉得第一种是最标准的写法吧, |
|
h****n 发帖数: 362 | 5 editDistance2(a, sizea - 1, b, sizeb -1 ) + (a[sizea-1]==b
[sizeb-1]? 0:1)); |
|