-
Notifications
You must be signed in to change notification settings - Fork 0
/
3-2.cpp
57 lines (52 loc) · 1.02 KB
/
3-2.cpp
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
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int min(int a, int b)
{
return (a<b ? a : b);
}
int edit(string a, string b)
{
int lena = a.length();
int lenb = b.length();
//int p[lena+1][lenb+1];
int **p = new int*[lena + 1];
for (int i = 0; i<lena + 1; i++)
{
p[i] = new int[lenb + 1];
}
for (int i = 0; i<lena + 1; i++)
p[i][0] = i;
for (int j = 0; j<lenb + 1; j++)
p[0][j] = j;
for (int i = 1; i <= lena; i++)
for (int j = 1; j <= lenb; j++)
{
int tmp = min(p[i - 1][j] + 1, p[i][j - 1] + 1);
int tmp1;
if (a[i - 1] == b[j - 1])
tmp1 = p[i - 1][j - 1];
else
tmp1 = p[i - 1][j - 1] + 1;
p[i][j] = min(tmp, tmp1);
}
return p[lena][lenb];
}
int main()
{
string a, b;
ifstream infile("input.txt", ios::in);
infile >> a >> b;
infile.close();
ofstream outfile("output.txt", ios::out);
outfile << edit(a, b) << endl;
outfile.close();
system("pause");
return 0;
/*string a, b;
cin >> a >> b;
cout << edit(a, b) << endl;
system("pause");
return 0;*/
}