1. 程式人生 > >poj——1080(dp)

poj——1080(dp)

解析:dp[i][j]來由:

1.dp[i][j-1]轉移,則‘-’和s2[j]匹配。

2.dp[i-1][j]轉移,則s1[i]和‘-’匹配。

3.dp[i-1][j-1],則s1[i]和s2[j]匹配。三者取最大值即可。

#include <iostream>
#include <cmath>
#include <string>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <cstdio>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
using namespace std;
typedef long long ll;
#define INF 0x7fffffff
#define MAX(a,b) a>b?a:b
#define MIN(a,b) a>b?b:a
#define N 103
int len2,len1;
int  dp[N][N];
char s1[N],s2[N];
int  match[N][N];
void init(){
	match['A']['T']=-1;match['T']['A']=-1;match['A']['A']=5;match['T']['T']=5;
	match['A']['C']=-1;match['C']['A']=-1;match['A']['G']=-2;match['G']['A']=-2;
	match['C']['G']=-3;match['G']['C']=-3;match['G']['G']=5;match['C']['C']=5;
	match['C']['T']=-2;match['T']['C']=-2;match['T']['G']=-2;match['G']['T']=-2;
	match['A']['-']=-3;match['-']['A']=-3;match['-']['C']=-4;match['C']['-']=-4;
	match['G']['-']=-2;match['-']['G']=-2;match['-']['T']=-1;match['T']['-']=-1;
}
void solve(){
	int i,j;
	dp[0][0]=0;
	for(i=1;i<=len1;i++)
	  dp[i][0]=dp[i-1][0]+match[s1[i]]['-'];
    for(i=1;i<=len2;i++)
      dp[0][i]=dp[0][i-1]+match['-'][s2[i]];
    for(i=1;i<=len1;i++)
     for(j=1;j<=len2;j++) 
     {
     	dp[i][j]=max(dp[i-1][j-1]+match[s1[i]][s2[j]],max(dp[i-1][j]+match[s1[i]]['-'],dp[i][j-1]+match['-'][s2[j]]));
     }
     cout<<dp[len1][len2]<<endl;
}
int main()
{
	  int t;
	  cin>>t;
	  init();
      while(t--){
      	cin>>len1>>s1+1;
      	cin>>len2>>s2+1;
      	solve();
      }
	return 0;
} 


相關推薦

poj——1080dp

解析:dp[i][j]來由: 1.dp[i][j-1]轉移,則‘-’和s2[j]匹配。 2.dp[i-1][j]轉移,則s1[i]和‘-’匹配。 3.dp[i-1][j-1],則s1[i]和s2[j

Poj 3046dp

Ant Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1954 Accepted: 822 Description Bessie was poking around the

POJ 1260-PearlsDP

ctype set lowest cas str pri font mount scan Pearls Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7465 Accepted

poj - 1088 - 滑雪dp

target art dsm 題目 ipp 每次 元素 org mod 題意:一個R * C的矩陣(1 <= R,C <= 100),元素代表該點的高度h(0<=h<=10000),從隨意點出發,每次僅僅能走上、下、左、右。且將要到的高度要比

POJ 題目1157 LITTLE SHOP OF FLOWERSDP

pop include ffffff suppose produce str tween cin 0ms LITTLE SHOP OF FLOWERS Time Limit: 1000MS Memory Limit: 10000K

POJ 1458DP初步_B題解題報告

con lap %d org for opened long hid body 題目鏈接:http://poj.org/problem?id=1458 -------------------------------------------------------- 題意:給

POJ 1321DP初步_I題解題報告

org sin return 題意 存在 分享圖片 problem aps 題目 題目鏈接:http://poj.org/problem?id=1321 -------------------------------------------------------- 題意:

POJ 1080 LCS變形

http org ext data lang mine image char mat 題目鏈接: http://poj.org/problem?id=1080 Human Gene Functions Time Limit: 1000MS Memory Limi

H - Increasing Sequences POJ - 1239 dp好題

H - Increasing Sequences POJ - 1239 Given a string of digits, insert commas to create a sequence of strictly increasing numbers so as to minimize th

hdu 1081/poj 1050 最大子矩陣和dp

換成谷歌瀏覽器以後終於可以黏貼程式碼了,更新以後的markdown真心難用。。。 dp問題,先把給定的二維矩陣壓縮,變成一維矩陣,如此即可變成hdu1003,用動態規劃的思路求解即可 #include<iostream> #include<cm

POJ 3356 AGTCdp x串變成y串最小代價

題目:http://poj.org/problem?id=3356 題意: 給你x串和y串,讓求操作x串變成y串的最小次數 操作:插入、刪除、修改一個字元 思路:用dp[i][j]表示x的前i個字元變成y的前j個字元的最小次數 修改 dp[i][j] = dp[i-1][j-1]+1

POJ 1925】 Spidermandp

Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6806 Accepted: 1361 Description Dr. Octopus kidnapped Spiderman's girlf

POJ 2029】 Get Many Persimmon TreesDP

Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4024 Accepted: 2628 Description Seiji Hayashi had been a professor of t

poj——1159dp之最長公共子序列

注:C++執行時Runtime Error,G++過了。(這編譯器,真無語了)。 #include <iostream> #include <cmath> #include

POJ Cow Bowlingdp

Description The cows don’t use actual bowling balls when they go bowling. They each take a numb

POJ 1159dp+備忘錄

不用short會引起記憶體超限, 不用備忘錄方法會引起TLE 但基本思路還是如下: 遞迴求解。 也可以用動態規劃演算法,但我看了半天還是不好理解,都說的不是特別好,要是有哪位大牛有好的思路希望在評論區給予我幫助,感激不盡。 #include<io

poj 2904 The Mailboxes Manufacturers ProblemDP

The Mailboxes Manufacturers Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 947 Accepted: 685 Description In the

poj 3176 dp 金字塔

Cow Bowling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17271 Accepted: 11531 Description The cows don't use actual

POJ 2192 Zipper dp

 連結: http://poj.org/problem?id=2192  題意:就是給定三個字串A,B,C;判斷C能否由AB中的字元組成,同時這個組合後的字元順序必須是A,B中原來的順序,不能逆序;例

POJ 2581 Exact Change Onlydp

Boudreaux reached over and shook awake Thibodeaux, who had dozed off somewhere in New Mexico. "Where we at?" Thibodeaux groggily yawned.  "Not in Vegas, I