-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSES.cpp
71 lines (49 loc) · 1.27 KB
/
SES.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
#include <iostream>
#include <string>
using namespace std;
int main() {
string A, B;
string C, D;
// Input sequences
cin>>A>>B;
int N = A.length();
int M = B.length();
int max = N+M;
bool flag = false;
int V[max+1];
int *p = & V[(max+1)/2];
p[1] = 0;
for ( int d = 0 ; d <= N + M ; d++ )
{
for ( int k = -d ; k <= d ; k += 2 )
{
// down or right?
bool down = ( k == -d || ( k != d && p[ k - 1 ] < p[ k + 1 ] ) );
int kPrev = down ? k + 1 : k - 1;
// start point
int xStart = p[ kPrev ];
int yStart = xStart - kPrev;
// mid point
int xMid = down ? xStart : xStart + 1;
int yMid = xMid - k;
// end point
int xEnd = xMid;
int yEnd = yMid;
// follow diagonal
int snake = 0;
while ( (xEnd < N) && (yEnd < M) && (A[ xEnd ] == B[ yEnd ]) ) { xEnd++; yEnd++; snake++; }
// save end point
p[ k ] = xEnd;
// check for solution
if ( xEnd >= N && yEnd >= M ) // Solution has been found!
{
cout<<"Minimum edits required to convert string A into B is: "<< d << endl;
flag = true;
break;
}
}
if(flag)
break;
}
return 0;
}