pku3356---AGTC(做的最少修改动作,动态规划)
2009-01-09 11:09
316 查看
4228K | 0MS | GCC | 568B | 2009-01-09 11:00:31 |
数组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;
}
相关文章推荐
- pku 3356(一个源串变换成目标串的最少步数,仅增删改3个操作)
- PKU3356 AGTC (简单DP)
- pku 3356 AGTC
- pku 3356 AGTC
- [动态规划,tsp问题]pku2288 Islands and Bridges
- [算法] poj 3356 字符串的距离 AGTC
- [动态规划]Pku1160--Post Office
- 动态规划 最少拦截系统
- Pku acm 3356 AGTC 动态规划题目解题报告(十)
- POJ 3356 AGTC 解题报告(Edit Distance,DP)
- 线段树系列-pku-3468-A Simple Problem with Integers-区间修改区间求和
- 最少硬币问题(动态规划解决)
- 动态规划-最少硬币组合问题
- poj 3356 AGTC(dp,最小编辑距离)
- POJ-3356-AGTC
- poj 3356 AGTC(lcs 变形题)
- pku1239 Increasing Sequences (动态规划)
- pku 3468 线段树-又是成段修改
- pku3639 Exchange Rates (动态规划)
- 给一个整数数列,修改最少的数使得严格递增