1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::cin;
using std::min;
int main () {
string s; // ref
string t; // hyp
cout <<"\n \t" << "Enter first word." << "\n \t";
cin>>s;
cout << "\t" << "Enter second word." << "\n \t";
cin>>t;
int len=s.length();
int leen=t.length();
int* d=new int[len+1, leen+1];
int* b=new int[len+1,leen+1];
int i,j;
for(i=0; i<=len; i++)
d[i,0]=i;
for(j=0; j<=leen; j++)
d[0,j]=j;
for(i=1;i<=len;i++)
{
for(j=1;j<=leen;j++)
{
if( s[i-1]==t[j-1] )
{
d[i,j]= d[i-1,j-1];
if(d[i,j]==d[i-1,j]+1)
{
b[i,j]=d[i-1,j]+1;
}
else if(d[i,j]==d[i,j-1]+1)
{
b[i,j]=d[i,j-1]+1;
}
else
{
b[i,j];
}
}
else
{
int m=min( d[i-1,j],d[i,j-1]+1);
d[i,j]=min( m, t[i-1,j-1]+ (s[i-1] == t[j-1]));
if( d[i,j]==d[i-1,j]+1 )
{
b[i,j]=d[i-1,j]+1;
}
else if( d[i,j]==d[i,j-1]+1 )
{
b[i,j]=d[i,j-1]+1;
}
else
{
b[i,j];
}
}
}
}
cout<<"\t" << d[len,leen] << "\n";
cout <<"\t" << b[len,leen];
delete[] d;
delete[] b;
return 0;
}
|