Beispiel #1
0
int
main(void)
{
    int T, ncase, n, i, j;

    scanf("%d", &T);
    ncase = 1;
    while (T-- > 0) {
        scanf("%d%d", &M, &N);
        for (i = 0; i < M; i++)
            for (j = 0; j < N; j++) {
                scanf("%d", &n);
                uranium[i][j] = n + prev_col(uranium, i, j);
            }
        for (i = 0; i < M; i++)
            for (j = 0; j < N; j++) {
                scanf("%d", &n);
                radium[i][j] = n + prev_row(radium, i, j);
            }

       printf("Case %d: %d\n", ncase++, solve());
    }

    return 0;
}
Beispiel #2
0
int
solve(void)
{
    int dp[500][500];
    int i, j;

    for (i = 0; i < M; i++) {
        for (j = 0; j < N; j++) {
            dp[i][j] = MAX(
                radium[i][j] + prev_col(dp, i, j),
                uranium[i][j] + prev_row(dp, i, j));
        }
    }

    return dp[M-1][N-1];
}
Beispiel #3
0
    static unsigned int calculateLevenshteinDistance(const T_StrType& str1, const T_StrType& str2)
    {
        const std::size_t len1 = str1.size(), len2 = str2.size();
        std::vector<unsigned int> col(len2+1), prev_col(len2+1);

        for (unsigned int i=0; i<prev_col.size(); i++)
        {
            prev_col[i] = i;
        }

        for (unsigned int i=0; i<len1; i++)
        {
            col[0] = i + 1;
            for (unsigned int j=0; j<len2; j++)
            {
                col[j+1] = std::min(std::min(prev_col[j+1] + 1, col[j] + 1), prev_col[j] + (str1[i]==str2[j] ? 0 : 1));
            }
            col.swap(prev_col);
        }

        return prev_col[len2];
    }
Beispiel #4
0
qint16 BulkHeaderGroup::levenshteinDistance(const QString& a_compare1, const QString& a_compare2)
{
    const qint16 length1 = a_compare1.size();
    const qint16 length2 = a_compare2.size();
    QVector<qint16> curr_col(length2 + 1);
    QVector<qint16> prev_col(length2 + 1);

    // Prime the previous column for use in the following loop:
    for (qint16 idx2 = 0; idx2 < length2 + 1; ++idx2)
    {
        prev_col[idx2] = idx2;
    }

    for (qint16 idx1 = 0; idx1 < length1; ++idx1)
    {
        curr_col[0] = idx1 + 1;

        for (qint16 idx2 = 0; idx2 < length2; ++idx2)
        {
            const qint16 compare = a_compare1[idx1] == a_compare2[idx2] ? 0 : 1;

            curr_col[idx2 + 1] = qMin(qMin(curr_col[idx2] + 1, prev_col[idx2 + 1] + 1),
                    prev_col[idx2] + compare);
        }

#if defined(Q_OS_OS2) || defined(Q_OS_OS2EMX) // In qglobal.h
    QVector<qint16> swap_col(length2 + 1);

    swap_col = prev_col;
    prev_col = curr_col;
    curr_col = swap_col;
#else
     curr_col.swap(prev_col);
#endif
    }

    return prev_col[length2];
}