hdoj1080 Human Gene Functions (DP,map)
来源:互联网 发布:手机淘宝付款方式 编辑:程序博客网 时间:2024/06/05 12:04
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1080
题意:
给两个字符串,都只由A、T、C、G这四个字母中的任意个组成,可重复出现,字符串长度1<=n<=100;可以在两字符串的任意位置补上空格使两个字符串等长度,然后匹配两个字符串,两字符串相同位置任意两个字母匹配有固定权值,字母与空格匹配也有固定权值,不允许空格与空格匹配,将每个位置的匹配权值求和,求两个字符串的最大匹配值。
思路:
首先权值的存放用的map定义一个容器value,如value["AA"]=5,表示两个字符串中A与A匹配的权值为5;那么a字符串前i个字母和b字符串前j个字母的匹配的最大权值的状态转换方程为:dp[i][j]=max((dp[i-1][j-1]+value[str1]), max(dp[i][j-1]+value[str2],dp[i-1][j]+value[str3])),其中dp[i-1][j-1]+value[str1]表示a字符串的第i个字母与b字符串的第j个字母匹配,dp[i][j-1]+value[str2]表示a字符串的空格与b字符串的第j个字母匹配,dp[i-1][j]+value[str3])表示a字符串的第i个字母与b字符串的空格匹配;最终结果就存在dp[na][nb] 中(na / nb为字符串长度);这个转换方程初始状态涉及到dp[0][0],d[i][0],dp[0][j],也就是某个字符串的前i或j个字符一直在与空格匹配,这个状态需要先初始化。
#include <iostream>#include <stdio.h>#include <string>#include <string.h>#include <map>using namespace std;int max(int x,int y){return x>y?x:y;}int main(){int cas,na,nb,i,j;int dp[102][102]; char a[102],b[102];char str[3]="xx",str1[3]="xx",str2[3]="xx",str3[3]="xx";map<string,int>value;value["AA"]=5;value["AC"]=-1;value["AG"]=-2;value["AT"]=-1;value["A-"]=-3;value["CA"]=-1;value["CC"]=5;value["CG"]=-3;value["CT"]=-2;value["C-"]=-4;value["GA"]=-2;value["GC"]=-3;value["GG"]=5;value["GT"]=-2;value["G-"]=-2;value["TA"]=-1;value["TC"]=-2;value["TG"]=-2;value["TT"]=5;value["T-"]=-1;value["-A"]=-3;value["-C"]=-4;value["-G"]=-2;value["-T"]=-1;value["--"]=-10000; scanf("%d",&cas);a[0]='-';b[0]='-';while(cas--){memset(dp,0,sizeof(dp));scanf("%d %s %d %s",&na,a+1,&nb,b+1);dp[0][0]=0;for(i=1;i<=na;i++){str[0]=a[i],str[1]='-';dp[i][0]=dp[i-1][0]+value[str];}for(j=1;j<=nb;j++){str[0]='-',str[1]=b[j];dp[0][j]=dp[0][j-1]+value[str];}for(i=1;i<=na;i++){for(j=1;j<=nb;j++){ str1[0]=a[i],str1[1]=b[j];str2[0]='-',str2[1]=b[j];str3[0]=a[i],str3[1]='-';dp[i][j]=max((dp[i-1][j-1]+value[str1]), max(dp[i][j-1]+value[str2],dp[i-1][j]+value[str3]) );}}printf("%d\n",dp[na][nb]);}return 0;}
阅读全文
0 0
- hdoj1080 Human Gene Functions (DP,map)
- hdoj1080 Human Gene Functions(DP解决LCS问题!)
- Human Gene Functions--DP
- POJ1080 Human Gene Functions DP
- pku1080 Human Gene Functions DP
- Human Gene Functions poj1080 dp
- hdu1080 Human Gene Functions DP
- ZOJ 1027 Human Gene Functions (DP)
- hdu 1080 Human Gene Functions--DP
- POJ 1080 Human Gene Functions DP(LCS)
- POJ 1080 Human Gene Functions DP
- hdu 1080 Human Gene Functions(dp)
- HDU 1080 Human Gene Functions dp
- hdu 1080 dp Human Gene Functions
- hdu 1080 Human Gene Functions (dp)
- HDU 1080 Human Gene Functions (DP)
- (DP)HDU 1080 Human Gene Functions
- dp poj 1080 Human Gene Functions
- 基于TI的TCP/IP协议栈---NDK
- Libevent源码分析-----超时event的处理
- 解决EventBus中接收方法中无法更新UI的问题
- Spark-Streaming中累加
- Libevent源码分析-----Libevent时间管理
- hdoj1080 Human Gene Functions (DP,map)
- 冒泡排序
- dom4j读取xml文件,用extjs以树的形式显示
- 倍增法求LCA
- Libevent源码分析-----管理超时event
- Spring中自定义JSON注释
- Python高级编程--闭包与装饰器
- 照片成像原理
- 015-Java-006