C-Edit Distance
time limit: Ms .
Memory Limit: 65536KB
64-bit input/output format: %i64d &%I 64u SubmitStatus
Describe
Let x and y are both strings over some finite alphabet A. We would like to transform x into y allowing only operations given below:
- deletion: a letter with x is missing in y at a corresponding position.
- insertion: a letter with y is missing in x at a corresponding position.
- Change : letters at corresponding positions is distinct
Certainly, we would like to minimize the number of all possible operations.
Illustration
| | | | | | |
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 is distinct
This tells us, transform x = AGTCTGACGC into y = AGTAAGTAGGC We would being required to perform 5 Operations (2 changes, 2 deletions and 1 insertion). If we want to minimize the number operations, we should does it like
A g t a a g t a g g
| | | | | | |
A g t C t G * a C G C
and 4 moves would be required (3 changes and 1 deletion).
In this problem we would all consider strings x and y to is fixed, such that number of letters in x is m and the number of letters in yis n where n ≥ m.
Assign 1 as the cost of an operation performed. Otherwise, assign 0 if there is no operation performed.
Write a program This would minimize the number of possible operations to transform any string x into a string y.
Input
The input consists of the strings x and y prefixed by their respective lengths, which is within 100 0.
Output
An integer representing the minimum number of possible operations to transform any string x into a string Y.
Sample Input
Ten AGTCTGACGC11 AGTAAGTAGGC
Sample Output
4
The main topic: Give two strings x, Y, to find out the minimum number of operations from X-->y, can only delete, add, modify a character.
Problem Solving Ideas:
is also a classic problem in DP
D[I][J] represents the first string to the I position, and the second string to the J position of the shortest editing distance
D[I][J]
If A[I]==B[J]
D[i][j]=min (D[i-1][j-1],d[i-1][j]+1,d[i][j-1]);
Otherwise d[i][j]=min (d[i-1][j-1]+1,d[i-1][j]+1,d[i][j-1]);
Program code:
1#include <cstdio>2#include <iostream>3 using namespacestd;4 Const intm=1100;5 CharA[m],b[m];6 intD[m][m];7 intMain ()8 {9 intN,i,j,l,r;Ten while(SCANF ("%d%s", &l,a+1)!=EOF) One { Ascanf"%d%s", &r,b+1); - intlen=Max (l,r); - for(i=0; i<=len;i++) the { -c[n][0]=i; -d[0][i]=i; - } + for(i=1; i<=l;i++) - for(j=1; j<=r;j++) + { AD[i][j]=min (d[i-1][j]+1, d[i][j-1]+1); at if(a[i]==B[j]) -d[i][j]=d[i-1][j-1]; - Else -D[i][j]=min (d[i][j],d[i-1][j-1]+1); - } -printf"%d\n", D[l][r]); in } - return 0; to}
View Code
Dynamic planning--c editing minimum distance