-
Notifications
You must be signed in to change notification settings - Fork 0
/
uva164.cpp
110 lines (76 loc) · 1.48 KB
/
uva164.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <limits.h>
#include <list>
using namespace std;
typedef struct
{
char op;
int pos1;
int pos2;
} command;
string s1, s2;
int dp[25][25];
int edit(int i, int j)
{
if(dp[i][j] != -1)
return dp[i][j];
if(i == 0)
return dp[i][j] = 1 + edit(i, j-1);
if(j == 0)
return dp[i][j] = 1 + edit(i-1, j);
if(s1[i-1] == s2[j-1])
return dp[i][j] = edit(i-1, j-1);
int option1 = edit(i-1, j-1) + 1;
int option2 = edit(i-1, j) + 1;
int option3 = edit(i, j-1) + 1;
return dp[i][j] = min(min(option1, option2), option3);
}
void change(int i, int j)
{
//cout << s1 << endl;
if(i == 0 && j == 0)
return;
if(s1[i-1] == s2[j-1])
change(i-1, j-1);
else
{
if((i > 0 && j > 0) && dp[i][j] == dp[i-1][j-1] + 1) //replace
{
//s1[i-1] = s2[j-1];
printf("C%c%02d", s2[j-1], i);
change(i-1, j-1);
}
else if(i > 0 && dp[i][j] == dp[i-1][j] + 1) //delete
{
//s1.erase(i-1, 1);
printf("D%c%02d", s1[i-1], i);
change(i-1, j);
}
else if(j > 0 && dp[i][j] == dp[i][j-1] + 1) //insert
{
//s1.insert(i, 1, s2[j-1]);
printf("I%c%02d", s2[j-1], i + 1);
change(i, j-1);
}
}
}
int main()
{
while(cin >> s1)
{
if(s1 == "#")
break;
memset(dp, -1, sizeof(dp));
cin >> s2;
int rez = edit(s1.length(), s2.length());
change(s1.length(), s2.length());
cout << "E" << endl;
}
return 0;
}