您的位置:首页 > 其它

pku3356---AGTC(做的最少修改动作,动态规划)

2009-01-09 11:09 316 查看
4228K0MSGCC568B2009-01-09 11:00:31
注意:这道题目是多case。。。。害我在这上面wrong了n次。

数组best[i][j],第一个字符串前i位和第二个字符串前j位能做到的最优值。

分析:

1)a[i]==b[j],best[i][j]=best[i-1][j-1];

2) a[i]!=b[j],best[i][j]=min(best[i-1][j-1],best[i-1][j],best[i][j-1])+1;

第一个元素:修改操作。第二个元素:删除操作。第三个元素:添加操作。

代码如下:

Code

#include<stdio.h>

#define min(a,b,c) ((a<b?a:b)<c?(a<b?a:b):c)

char a[1005],b[1005];

int lena,lenb,best[1005][1005];

void process()

{

int i,j;

for(i=0;i<=lena;i++)

best[i][0]=i;

for(j=1;j<=lenb;j++)

best[0][j]=j;

for(i=1;i<=lena;i++)

for(j=1;j<=lenb;j++){

if(a[i]==b[j])

best[i][j]=best[i-1][j-1];

else

best[i][j]=min(best[i-1][j-1],best[i-1][j],best[i][j-1])+1;

}

printf("%d\n",best[lena][lenb]);

}

int main()

{

while(scanf("%d %s",&lena,&a[1])!=EOF){

scanf("%d %s",&lenb,&b[1]);

process();

}

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: