[题目来源]:Taejon 2001
[关键字]:动态规划
[题目大意]:给出两个基因序列,可以在其中插入‘-',根据给出的每个字母对应的值,求出使这两个变得长度相等所需的最大值。
//=====================================================================================================
[分析]:就是LCS的变形,其中f[i,j]=max(f[i-1,j-1]+a[ord(s1[i]),ord(s2[j])],f[i-1,j]+a[ord(s1[i],'-')],f[i,j-1]+a['-',ord(s2[j])]),最后所求就是f[n,m]
[代码]:
View Code
1 program Project1;
2 const
3 map: array[1..5,1..5] of longint=((5,-1,-2,-1,-3),
4 (-1,5,-3,-2,-4),
5 (-2,-3,5,-2,-2),
6 (-1,-2,-2,5,-1),
7 (-3,-4,-2,-1,0));
8 var
9 n, m, tc: longint;
10 s1, s2: string;
11 f: array[0..200,0..200] of longint;
12
13 procedure init;
14 var
15 ch: char;
16 begin
17 read(n,ch);
18 readln(s1);
19 read(m,ch);
20 readln(s2);
21 //delete(s1,length(s1),1);
22 //delete(s2,length(s2),1);
23 //writeln(s1,'#',n);
24 //writeln(s2,'#',m);
25 end;
26
27 function dis(ch1, ch2: char):longint;
28 var
29 x, y: longint;
30 begin
31 case ch1 of
32 'A':x := 1;
33 'C':x := 2;
34 'G':x := 3;
35 'T':x := 4;
36 '-':x := 5;
37 end;
38 case ch2 of
39 'A':y := 1;
40 'C':y := 2;
41 'G':y := 3;
42 'T':y := 4;
43 '-':y := 5;
44 end;
45 //writeln(ch1,'',ch2,'',x,'',y,'',map[x,y]);
46 exit(map[x,y]);
47 end;
48
49 function max(x, y, z: longint):longint;
50 begin
51 max := x;
52 if max < y then max := y;
53 if max < z then max := z;
54 end;
55
56 procedure work;
57 var
58 i, j: longint;
59 begin
60 fillchar(f,sizeof(f),0);
61 for i := 1 to n do
62 f[i,0] := dis(s1[i],'-')+f[i-1,0];
63 for i := 1 to m do
64 f[0,i] := dis('-',s2[i])+f[0,i-1];
65 {for i := 1 to n do
66 writeln(i,' ',0,' ',f[i,0]);}
67 for i := 1 to n do
68 for j := 1 to m do
69 begin
70 f[i,j] := max(f[i-1,j-1]+dis(s1[i],s2[j]),f[i-1,j]+dis(s1[i],'-'),f[i,j-1]+dis('-',s2[j]));
71 // writeln(i,'',j,'',f[i,j]);
72 // writeln(i,'',j,'',f[i-1,j-1]+dis(s1[i],s2[j]),'',f[i-1,j]+dis(s1[i],'-'),'',f[i,j-1]+dis('-',s2[j]),'',f[i,j]);
73 end;
74 writeln(f[n,m]);
75 end;
76
77 begin
78 readln(tc);
79 while tc > 0 do
80 begin
81 init;
82 work;
83 dec(tc);
84 end;
85 end.