-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUntitled8.cpp
97 lines (77 loc) · 1.55 KB
/
Untitled8.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
#include <stdio.h>
#include <string.h>
#include <iostream>
typedef struct B{
bool a;
bool b;
}B;
B dp[17000][17000];
using namespace std;
string str1,str2;
int arr[2][17000];
void print(int x,int y){
if(x==0&&y==0){
return;
}
if(dp[x][y].a==false&&dp[x][y].b==false){
print(x-1,y-1);
printf("c %c\n",str1[x-1]);
}else if(dp[x][y].a==false){
print(x,y-1);
printf("a %c\n",str2[y-1]);
}else if(dp[x][y].b==false){
print(x-1,y);
printf("d %c\n",str1[x-1]);
}else{
print(x-1,y-1);
printf("m %c\n",str2[y-1]);
}
}
// 1 0 north d
// 0 1 west a
// 1 1 m
int main(){
cin>>str1;
cin>>str2;
for(int i=1;i<=str1.length();i++){
dp[i][0].a = true;
}
for(int i=1;i<=str2.length();i++){
dp[0][i].b= true;
arr[0][i]=i;
arr[1][i]=i;
}
for(int i=1;i<=str1.length();i++){
memcpy(arr[0],arr[1],sizeof(arr[0]));
arr[0][0]=i-1;
arr[1][0]=i;
for(int j=1;j<=str2.length();j++){
if(str1[i]==str2[j]){
arr[1][j]=arr[0][j-1];
}else{
if((arr[0][j-1]<arr[0][j])&&(arr[0][j-1]<arr[1][j-1])){//m
dp[i][j].a=true;
dp[i][j].b=true;
arr[1][j]=arr[0][j-1]+1;
}else if(arr[0][j]<arr[1][j-1]){//d
dp[i][j].a=true;
arr[1][j]=arr[0][j]+1;
}else{//a
arr[1][j]=arr[1][j-1]+1;
dp[i][j].b=true;
}
}
}
// for(int i=0;i<=str2.length();i++){
// printf("%d ",arr[1][i]);
// }
// printf("\n");
}
print(str1.length(),str2.length());
// for(int i=0;i<str1.length();i++){
// for(int j=0;j<str2.length();j++){
// printf("%d ",dp[i][j]);
// }
// printf("\n");
// }
}