[ create a new paste ] login | about

Link: http://codepad.org/y3l62zSX    [ raw code | output | fork ]

C++, pasted on Jan 22:
#include <sstream>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <string.h>
typedef long long ll;

#define pb          push_back
#define inf         99999999
#define SZ          10000000
#define LD          long double
#define VS          vector<string>
#define VI          vector<int>
#define VD          vector<double>
#define VLL         vector<ll>
#define pb          push_back
#define pi          2*acos(0)
#define sz          size()
#define mem(a,b)    memset(a,b,sizeof(a))

using namespace std;

ll i,j,path[2010][2010],dp[2100][2100],cnt;
string str1,str2,str;

void prn(ll i,ll j)
{
    if(i==0 || j==0)return;
    if(path[i][j]==1)
    {
        prn(i-1,j-1);
        ++cnt;
    }
    else
    {
        if(path[i][j]==2)
        {
            prn(i-1,j);
            cout<<"DELETE "<<cnt+1<<endl;
        }
        else if(path[i][j]==3)
        {
            prn(i,j-1);
            cout<<"INSERT "<<++cnt<<" "<<str2[j]<<endl;
        }
        else
        {
            prn(i-1,j-1);
            cout<<"REPLACE "<<++cnt<<" "<<str2[j]<<endl;
        }
    }
}
int EditDistance()
{
    ll i,j,cost;
    str1=" "+str1;
    str2=" "+str2;
    for(i=0;i<str1.size();i++)
        dp[i][0]=i;
    for(j=0;j<str2.size();j++)
        dp[0][j]=j;

    for(i=1;i<str1.size();i++)
        for(j=1;j<str2.size();j++)
        {
            if(str1[i]==str2[j])
            {
                dp[i][j]=dp[i-1][j-1];
                path[i][j]=1;
            }
            else
            {
                ll a = dp[i][j];
                dp[i][j]=min(dp[i-1][j]+1,dp[i][j-1]+1);
                a = dp[i][j];
                if(a==dp[i-1][j]+1)path[i][j] = 2;
                else path[i][j]=3;
                dp[i][j]=min(dp[i][j],dp[i-1][j-1]+1);
                if(a!=dp[i][j])
                {
                    path[i][j]=4;
                }
            }
        }
    //deletion dp[i-1][j]+1 2
    //insertion dp[i][j-1]+1  3
    //substitution dp[i-1][j-1]+1  4
    return dp[i-1][j-1];
}
int main()
{
    //freopen("in.txt","r",stdin);
    cin>>str1;
    cin>>str2;
    cout<<EditDistance()<<endl;
    cnt=0;
    prn(str1.sz-1,str2.sz-1);
    return 0;
}


Output:
1
2
Line 14: error: ISO C++ does not support 'long long'
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: