Description
LetXAndYBe two strings over some finite alphabetA. We wowould like to transformXIntoYAllowing only operations given below:
- Deletion:A letter inXIs missing inYAt a corresponding position.
- Insertion:A letter inYIs missing inXAt a corresponding position.
- Change:Letters at corresponding positions are distinct
Certainly, we wowould like to minimize the number of all possible operations.
Statement
A G T A A G T * A G G C| | | | | | |A G T * C * T G A C G C
Deletion:* In the bottom line
Insertion:* In the top line
Change:When the letters at the top and bottom are distinct
This tells us that to transformX= AgtctgacgcY= Agtaagtaggc we wocould be required to perform 5 operations (2 changes, 2 deletions and 1 insertion). If we want to minimize the number operations, we shocould do it like
A G T A A G T A G G C| | | | | | |A G T C T G * A C G C
And 4 moves wocould be required (3 changes and 1 deletion ).
In this problem we wowould always consider stringsXAndYTo be fixed, such that the number of letters inXIsMAnd the number of letters inYIsNWhereN≥M.
Assign 1 as the cost of an operation performed med. Otherwise, assign 0 if there is no operation performed med.
Write a program that wowould Minimize the number of possible operations to transform any stringXInto a stringY.
Input
The input consists of the stringsXAndYPrefixed by their respective lengths, which are within 1000.
Output
An integer representing the minimum number of possible operations to transform any stringXInto a stringY.
Sample Input
10 AGTCTGACGC11 AGTAAGTAGGC
Sample output
4
Set DP [I] [J] to the minimum number of times that the first string ends with I and the second string ends with J.
DP [I] [J] = DP [I-1] [J-1] (s [I] = s [J])
DP [I] [J] = max {DP [I] [J], DP [I-1] [J] + 1, DP [I] [J-1] + 1, DP [I-1] [J-1] + 1}
# Include <stdio. h>
# Include <string. h>
# Include <algorithm>
# Include <math. h>
# Define lson o <1, l, m
# Define rson o <1 | 1, m + 1, R
Using namespace STD;
Typedef long ll;
Const int max = 0x3f3f3f;
Const int maxn = 1000 + 10;
Int n, m, DP [maxn] [maxn];
Char A [maxn], B [maxn];
Int min (int x, int y, int Z, int W ){
Int ans = max;
If (x <ans) ans = X;
If (Y <ans) ans = y;
If (z <ans) ans = z;
If (W <ans) ans = W;
Return ans;
}
Int main ()
{
While (~ Scanf ("% d", & N )){
Scanf ("% s", A + 1 );
Scanf ("% d % s", & M, B + 1 );
Memset (DP, Max, sizeof (DP ));
For (INT I = 0; I <= m; I ++) DP [0] [I] = I;
For (INT I = 0; I <= N; I ++) DP [I] [0] = I;
For (INT I = 1; I <= N; I ++)
For (Int J = 1; j <= m; j ++ ){
If (A [I] = B [J]) DP [I] [J] = DP [I-1] [J-1];
DP [I] [J] = min (DP [I] [J], DP [I-1] [J-1] + 1, DP [I-1] [J] + 1, DP [I] [J-1] + 1 );
}
Printf ("% d \ n", DP [N] [m]);
}
Return 0;
}
Zookeeper