poj1080
来源:互联网 发布:linux查看服务运行状态 编辑:程序博客网 时间:2024/06/15 16:01
动态规划,LCS的扩展;
f[i][j]为a[i]与b[j]的相似值,aa[i],bb[i]是a[i],b[j]的坐标;
动态方程是:f[i][j]=max{f[i-1][j-1]+score[aa[i-1]][bb[j-1]],f[i][j-1]+score[4][bb[j-1]],f[i-1][j]+score[aa[i-1]][4]};
最后要注意边界:当i==0&&j==0时,f[i][j]=0;
i=0,f[i][j]=f[i][j]=f[i][j-1]+score[4][bb[j-1]];
j=0,f[i][j]=f[i-1][j]+score[aa[i-1]][4];
在写代码的时候要细心,我因为弄错了一个位置,纠结了好尝试时间,也wn了好几次,呵呵。。。所以大家一定要注意啊 !!!!
代码如下:
#include<stdio.h>#include<string.h>int la,lb;char a[101],b[101];int aa[101],bb[101];int f[101][101];int score[5][5]={{5,-1,-2,-1,-3},{-1,5,-3,-2,-4},{-2,-3,5,-2,-2},{-1,-2,-2,5,-1},{-3,-4,-2,-1,0}};void main(){ int N; scanf("%d",&N); while(N--) { int i,j,f1,f2,f3; memset(f,0,sizeof(f)); scanf("%d%s",&la,a); scanf("%d%s",&lb,b); for(i=0;i<la;i++) { if(a[i]=='A') aa[i]=0; if(a[i]=='C') aa[i]=1; if(a[i]=='G') aa[i]=2; if(a[i]=='T') aa[i]=3; if(a[i]=='-') aa[i]=4; } for(i=0;i<lb;i++) { if(b[i]=='A') bb[i]=0; if(b[i]=='C') bb[i]=1; if(b[i]=='G') bb[i]=2; if(b[i]=='T') bb[i]=3; if(b[i]=='-') bb[i]=4; } for(i=0;i<=la;i++) { for(j=0;j<=lb;j++) { if(i==0 && j==0) f[i][j]=0; else if(i==0) f[i][j]=f[i][j-1]+score[4][bb[j-1]]; else if(j==0) f[i][j]=f[i-1][j]+score[aa[i-1]][4]; else { f1=f[i-1][j-1]+score[aa[i-1]][bb[j-1]];f2=f[i][j-1]+score[4][bb[j-1]];f3=f[i-1][j]+score[aa[i-1]][4];f[i][j]=(f1>f2?f1:f2)>f3?(f1>f2?f1:f2):f3; } } } printf("%d\n",f[la][lb]); }}
希望对大家有一定的帮助!!!!!
- poj1080
- poj1080
- poj1080
- poj1080
- poj1080
- poj1080
- POJ1080
- poj1080
- poj1080
- poj1080
- poj1080
- poj1080
- [DP] poj1080
- poj1080 dp
- POJ1080 DP
- poj1080(dp)
- Human Gene Functions(POJ1080)
- POJ1080 - DP(LCS变种)
- HDOJ 1443 约瑟夫环的最新应用
- Poj DancingLinks(3372 3074 3076)
- a weird result when I try to print the value of point by %p format
- CRLF -- Carriage-Return Line-Feed 回车换行
- C++设计模式-原型
- poj1080
- ORA-00845: MEMORY_TARGET not supported on this system
- 笔记本配置
- Java学习问题1: boolean型数据占几个字节?
- hdu 3893 Drawing Pictures
- C# 组件,动态创建的组件,本身引起的异常
- TCP协议和UDP协议有什么不同?
- 非阻塞IO 和阻塞IO
- 如何隐藏QT窗口在任务栏(taskbar)的任务图标