Skip to main content
edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
Post Unlocked by Adam
Notice removed Content dispute by Adam
Post Locked by Jamal
Notice added Content dispute by Jamal
Rollback to Revision 6
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
#include <cassert>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

constinline size_t insert_costmin(size_t =x, 1;
constsize_t y, size_t delete_costz)
{
 = 1;  if (x < y)
const size_t replace_cost = 2;    return x < z ? x : z;
    else
        return y < z ? y : z;
}

size_t edit_distance(const string& A, const string& B)
{
    size_t NA = A.size();
    size_t NB = B.size();

    vector<size_t>vector<vector<size_t>> M0M(NB+1)NA + 1, M1vector<size_t>(NB+1NB + 1));

    for (size_t ba = 0; ba <= NB;NA; ++b++a)
        M0[b]M[a][0] = b * delete_cost;a;

    for (size_t ab = 1;0; ab <= NA;NB; ++a++b)
    {
    M[0][b] = b;

    M1[0]for (size_t a = 1; a *<= insert_cost;
NA; ++a)
        for (size_t b = 1; b <= NB; ++b)
        {
            size_t x = M0[b]M[a-1][b] + insert_cost;1;
            size_t y = M1[bM[a][b-1] + delete_cost;1;
            size_t z = M0[bM[a-1][b-1] + (A[a-1] == B[b-1] ? 0 : replace_cost2);
            M1[b]M[a][b] = min({x,y,z});
        }

       return swapM[A.size(M0,M1);
    }

    return M0[NB];][B.size()];
}

int main()
{
    assert(edit_distance("ISLANDER", "SLANDER") == 1);
    assert(edit_distance("MART", "KARMA") == 5);
    assert(edit_distance("KITTEN", "SITTING") == 5);
    assert(edit_distance("INTENTION", "EXECUTION") == 8);
}
#include <cassert>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

const size_t insert_cost = 1;
const size_t delete_cost = 1;
const size_t replace_cost = 2;

size_t edit_distance(const string& A, const string& B)
{
    size_t NA = A.size();
    size_t NB = B.size();

    vector<size_t> M0(NB+1), M1(NB+1);

    for (size_t b = 0; b <= NB; ++b)
        M0[b] = b * delete_cost;

    for (size_t a = 1; a <= NA; ++a)
    {
        M1[0] = a * insert_cost;

        for (size_t b = 1; b <= NB; ++b)
        {
            size_t x = M0[b] + insert_cost;
            size_t y = M1[b-1] + delete_cost;
            size_t z = M0[b-1] + (A[a-1] == B[b-1] ? 0 : replace_cost);
            M1[b] = min({x,y,z});
        }

        swap(M0,M1);
    }

    return M0[NB];
}

int main()
{
    assert(edit_distance("ISLANDER", "SLANDER") == 1);
    assert(edit_distance("MART", "KARMA") == 5);
    assert(edit_distance("KITTEN", "SITTING") == 5);
    assert(edit_distance("INTENTION", "EXECUTION") == 8);
}
#include <cassert>
#include <string>
#include <vector>
using namespace std;

inline size_t min(size_t x, size_t y, size_t z)
{
    if (x < y)
        return x < z ? x : z;
    else
        return y < z ? y : z;
}

size_t edit_distance(const string& A, const string& B)
{
    size_t NA = A.size();
    size_t NB = B.size();

    vector<vector<size_t>> M(NA + 1, vector<size_t>(NB + 1));

    for (size_t a = 0; a <= NA; ++a)
        M[a][0] = a;

    for (size_t b = 0; b <= NB; ++b)
        M[0][b] = b;

    for (size_t a = 1; a <= NA; ++a)
        for (size_t b = 1; b <= NB; ++b)
        {
            size_t x = M[a-1][b] + 1;
            size_t y = M[a][b-1] + 1;
            size_t z = M[a-1][b-1] + (A[a-1] == B[b-1] ? 0 : 2);
            M[a][b] = min(x,y,z);
        }

    return M[A.size()][B.size()];
}

int main()
{
    assert(edit_distance("ISLANDER", "SLANDER") == 1);
    assert(edit_distance("MART", "KARMA") == 5);
    assert(edit_distance("KITTEN", "SITTING") == 5);
    assert(edit_distance("INTENTION", "EXECUTION") == 8);
}
Rollback to Revision 4
Source Link
#include <cassert>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

inline size_t min(size_t x, size_t y,const size_t z)
{
    if (x < y)
        return x < z ? xinsert_cost := z;1;
const size_t delete_cost = else1;
        return y < zconst ?size_t yreplace_cost := z;
}2;

size_t edit_distance(const string& A, const string& B)
{
    size_t NA = A.size();
    size_t NB = B.size();

    vector<vector<size_t>>vector<size_t> MM0(NA + 1NB+1), vector<size_t>M1(NB + 1)NB+1);

    for (size_t ab = 0; ab <= NA;NB; ++a++b)
        M[a][0]M0[b] = a;b * delete_cost;

    for (size_t ba = 0;1; ba <= NB;NA; ++b++a)
       {
 M[0][b] = b;

    for (size_t aM1[0] = 1; a <= NA;* ++a)insert_cost;

        for (size_t b = 1; b <= NB; ++b)
        {
            size_t x = M[a-1][b]M0[b] + 1;insert_cost;
            size_t y = M[a][bM1[b-1] + 1;delete_cost;
            size_t z = M[a-1][bM0[b-1] + (A[a-1] == B[b-1] ? 0 : 2replace_cost);
            M[a][b]M1[b] = min({x,y,z});
        }

    return M[A.size()][B.size   swap(M0,M1)];;
    }

    return M0[NB];
}

int main()
{
    assert(edit_distance("ISLANDER", "SLANDER") == 1);
    assert(edit_distance("MART", "KARMA") == 5);
    assert(edit_distance("KITTEN", "SITTING") == 5);
    assert(edit_distance("INTENTION", "EXECUTION") == 8);
}
#include <cassert>
#include <string>
#include <vector>
using namespace std;

inline size_t min(size_t x, size_t y, size_t z)
{
    if (x < y)
        return x < z ? x : z;
    else
        return y < z ? y : z;
}

size_t edit_distance(const string& A, const string& B)
{
    size_t NA = A.size();
    size_t NB = B.size();

    vector<vector<size_t>> M(NA + 1, vector<size_t>(NB + 1));

    for (size_t a = 0; a <= NA; ++a)
        M[a][0] = a;

    for (size_t b = 0; b <= NB; ++b)
        M[0][b] = b;

    for (size_t a = 1; a <= NA; ++a)
        for (size_t b = 1; b <= NB; ++b)
        {
            size_t x = M[a-1][b] + 1;
            size_t y = M[a][b-1] + 1;
            size_t z = M[a-1][b-1] + (A[a-1] == B[b-1] ? 0 : 2);
            M[a][b] = min(x,y,z);
        }

    return M[A.size()][B.size()];
}

int main()
{
    assert(edit_distance("ISLANDER", "SLANDER") == 1);
    assert(edit_distance("MART", "KARMA") == 5);
    assert(edit_distance("KITTEN", "SITTING") == 5);
    assert(edit_distance("INTENTION", "EXECUTION") == 8);
}
#include <cassert>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

const size_t insert_cost = 1;
const size_t delete_cost = 1;
const size_t replace_cost = 2;

size_t edit_distance(const string& A, const string& B)
{
    size_t NA = A.size();
    size_t NB = B.size();

    vector<size_t> M0(NB+1), M1(NB+1);

    for (size_t b = 0; b <= NB; ++b)
        M0[b] = b * delete_cost;

    for (size_t a = 1; a <= NA; ++a)
    {
        M1[0] = a * insert_cost;

        for (size_t b = 1; b <= NB; ++b)
        {
            size_t x = M0[b] + insert_cost;
            size_t y = M1[b-1] + delete_cost;
            size_t z = M0[b-1] + (A[a-1] == B[b-1] ? 0 : replace_cost);
            M1[b] = min({x,y,z});
        }

        swap(M0,M1);
    }

    return M0[NB];
}

int main()
{
    assert(edit_distance("ISLANDER", "SLANDER") == 1);
    assert(edit_distance("MART", "KARMA") == 5);
    assert(edit_distance("KITTEN", "SITTING") == 5);
    assert(edit_distance("INTENTION", "EXECUTION") == 8);
}
edited tags
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481
Loading
Rollback to Revision 1
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
Tweeted twitter.com/#!/StackCodeReview/status/194201406985617408
added 28 characters in body
Source Link
Loading
added 217 characters in body
Source Link
Loading
deleted 12 characters in body
Source Link
Loading
Source Link
Loading