码迷,mamicode.com
首页 > 其他好文 > 详细

poj3356 AGTC

时间:2015-08-15 16:13:32      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

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[1001][1001];
string a,b;
int n,m;
int Dp(int i,int j){
    if(dp[i][j]==-1){
        int t1=Dp(i-1,j)+1;
        int t2=Dp(i,j-1)+1;
        int t3=Dp(i-1,j-1)+(a[i-1]==b[j-1]?0:1);
        dp[i][j]=min(min(t1,t2),t3);
    }
    return dp[i][j];
}
int main(){
    while(cin>>n>>a>>m>>b){
        memset(dp,-1,sizeof(dp));
        int t=max(n,m);
        for(int i=0;i<=t;i++){
            dp[0][i]=i;
            dp[i][0]=i;
        }
        cout<<Dp(n,m)<<endl;
    }
return 0;
}
View Code

 

poj3356 AGTC

标签:

原文地址:http://www.cnblogs.com/demodemo/p/4732482.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!