poj3356 AGTC

Description

Let x and y be two strings over some finite alphabet A. We would like to transform x into y allowing only operations given below:

  • Deletion: a letter in x is missing in y at a corresponding position.
  • Insertion: a letter in y is missing in x at a corresponding position.
  • Change: letters at corresponding positions are distinct

Certainly, we would like to minimize the number of all possible operations.

Illustration

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 transform x = AGTCTGACGC into y = AGTAAGTAGGC we would be required to perform 5 operations (2 changes, 2 deletions and 1 insertion). If we want to minimize the number operations, we should 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 would be required (3 changes and 1 deletion).

In this problem we would always consider strings x and y to be fixed, such that the number of letters in x is m and the number of letters in y is nwhere n ≥ m.

Assign 1 as the cost of an operation performed. Otherwise, assign 0 if there is no operation performed.

Write a program that 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 are within 1000.

Output

An integer representing the minimum number of possible operations to transform any string x into a string y.

Sample Input

10 AGTCTGACGC
11 AGTAAGTAGGC

Sample Output

4

 题意: 把一个字符串经过最少操作步数转为另一个字符串 ——------操作可以是删除插入修改一个字符

dp[i][j]表示A[0-i] B[0-j]相等的最少步数

我们先来对B进行操作

删除的B[j]   :   d[i][j]=d[i][j-1]+1;

在B[j]后面插入一个: d[i][j]=d[i-1][j]+1;

删除一个数 if(B[j]==A[i]) d[i][j]=d[i-1][j-1];

else  d[i][j]=d[i-1][j-1]+1;

求以上三种方法的最大d[i][j];

同理对A[i]操作也方程也是不变的

#include<iostream>
#include<cstring>
#include<string>
using namespace std;
int dp[][];
string a,b;
int n,m;
int Dp(int i,int j){
if(dp[i][j]==-){
int t1=Dp(i-,j)+;
int t2=Dp(i,j-)+;
int t3=Dp(i-,j-)+(a[i-]==b[j-]?:);
dp[i][j]=min(min(t1,t2),t3);
}
return dp[i][j];
}
int main(){
while(cin>>n>>a>>m>>b){
memset(dp,-,sizeof(dp));
int t=max(n,m);
for(int i=;i<=t;i++){
dp[][i]=i;
dp[i][]=i;
}
cout<<Dp(n,m)<<endl;
}
return ;
}
上一篇:Python——教你画朵太阳花


下一篇:CEYE平台的使用