Skip to content

Commit d80e8f0

Browse files
committed
tco 04 semi round 1 500
1 parent d1cfdc3 commit d80e8f0

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

BioScore.cpp

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <queue>
8+
#include <cstring>
9+
#include <climits>
10+
#include <cstdio>
11+
using namespace std;
12+
typedef pair<int, int> pi;
13+
typedef pair<char, char> pc;
14+
typedef vector<int> vi;
15+
typedef vector<vi> vvi;
16+
typedef vector<string> vs;
17+
typedef vector<vs> vvs;
18+
19+
class BioScore{
20+
public:
21+
22+
double maxAvg(vector <string> knownSet) {
23+
24+
vi ex(10);
25+
int n = knownSet.size();
26+
map<pc, int> P, D;
27+
P[make_pair('A', 'C')] = P[make_pair('C', 'A')] = 0;
28+
P[make_pair('A', 'T')] = P[make_pair('T', 'A')] = 1;
29+
P[make_pair('A', 'G')] = P[make_pair('G', 'A')] = 2;
30+
P[make_pair('T', 'C')] = P[make_pair('C', 'T')] = 3;
31+
P[make_pair('G', 'C')] = P[make_pair('C', 'G')] = 4;
32+
P[make_pair('G', 'T')] = P[make_pair('T', 'G')] = 5;
33+
34+
P[make_pair('A', 'A')] = 6;
35+
P[make_pair('C', 'C')] = 7;
36+
P[make_pair('T', 'T')] = 8;
37+
P[make_pair('G', 'G')] = 9;
38+
39+
for(int i = 0; i < n; i++)
40+
for(int j = i+1; j < n; j++){
41+
string s = knownSet[i], t = knownSet[j];
42+
for(int k = 0; k < s.size(); k++)
43+
ex[P[make_pair(s[k], t[k])]]++;
44+
}
45+
46+
sort(ex.begin(), ex.begin()+6);
47+
48+
int ret = -1000000000;
49+
for(int i = 1; i <= 10; i++)
50+
for(int j = 1; j <= 10; j++)
51+
for(int k = 1; k <= 10; k++)
52+
for(int l = 1; l <= 10; l++)
53+
if((i+j+k+l)%2 == 0){
54+
int t = 10-(i+j+k+l)/2;
55+
int v = (-10*ex[0]-10*ex[1]-10*ex[2]+t*ex[3]+10*ex[4]+10*ex[5]) + i*ex[6]+j*ex[7]+k*ex[8]+l*ex[9];
56+
ret = max(ret, v);
57+
}
58+
59+
return (2*(double)ret)/(n*(n-1));
60+
}
61+
62+
// BEGIN CUT HERE
63+
public:
64+
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); }
65+
private:
66+
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
67+
void verify_case(int Case, const double &Expected, const double &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
68+
void test_case_0() { string Arr0[] = {"AAA","AAA","AAC"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); double Arg1 = 30.0; verify_case(0, Arg1, maxAvg(Arg0)); }
69+
void test_case_1() { string Arr0[] = {"ACTGACTGACTG","GACTTGACCTGA"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); double Arg1 = -4.0; verify_case(1, Arg1, maxAvg(Arg0)); }
70+
void test_case_2() { string Arr0[] = {"ACTAGAGAC","AAAAAAAAA","TAGTCATAC","GCAGCATTC"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); double Arg1 = 50.5; verify_case(2, Arg1, maxAvg(Arg0)); }
71+
72+
// END CUT HERE
73+
74+
};
75+
76+
// BEGIN CUT HERE
77+
int main(){
78+
79+
BioScore ___test;
80+
___test.run_test(-1);
81+
}
82+
// END CUT HERE

BioScore.html

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<html><body bgcolor="#000000" text="#ffffff"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td>
2+
Two equal length sequences of nucleic acids (A,C,T,or G) are judged based on
3+
a 4x4 score
4+
matrix. The homology score for the sequences is just the sum of the scores
5+
at each position in the sequence, looked up in the matrix.
6+
For example, if the two sequences were ACTA and GATC then their homology
7+
score would be score(A,G) + score(C,A) + score(T,T) + score(A,C).
8+
9+
<p>
10+
Our problem is to construct the score matrix. It has the following restrictions:
11+
</p><ul><li>
12+
1) All entries must be integers between -10 and 10 inclusive</li><li>
13+
2) It must be symmetric ( score(x,y) = score(y,x) )</li><li>
14+
3) Diagonal entries must be positive ( score(x,x)&gt;0 )</li><li>
15+
4) The sum of the 16 entries must be 0.</li></ul>
16+
17+
We are given a set of equal length sequences that are known to be homologous.
18+
We want to fill in the score matrix so that the average homology score for all
19+
the pairs from the set is maximized. If there are n sequences in the set, then
20+
there are n*(n-1)/2 pairs to consider.
21+
<p>
22+
Create a class BioScore that contains a method maxAvg that is given <b>knownSet</b>,
23+
the list of sequences known to be homologous. The method computes the optimum
24+
scoring matrix and returns the resulting maximal average homology score for the
25+
pairs from <b>knownSet</b>.
26+
</p>
27+
28+
29+
</td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Class:</td><td>BioScore</td></tr><tr><td>Method:</td><td>maxAvg</td></tr><tr><td>Parameters:</td><td>vector &lt;string&gt;</td></tr><tr><td>Returns:</td><td>double</td></tr><tr><td>Method signature:</td><td>double maxAvg(vector &lt;string&gt; knownSet)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td colspan="2"><h3>Limits</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Time limit (s):</td><td>2.000</td></tr><tr><td>Memory limit (MB):</td><td>64</td></tr></table></td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>knownSet</b> will contain between 2 and 50 elements inclusive</td></tr><tr><td align="center" valign="top">-</td><td>each element of <b>knownSet</b> will contain only the uppercase letters A,C,T, and G.</td></tr><tr><td align="center" valign="top">-</td><td>each element of <b>knownSet</b> will contain between 1 and 50 characters inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>all elements of <b>knownSet</b> will contain the same number of characters.</td></tr><tr><td colspan="2"><h3>Examples</h3></td></tr><tr><td align="center" nowrap="true">0)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{&quot;AAA&quot;,&quot;AAA&quot;,&quot;AAC&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 30.0</pre></td></tr><tr><td><table><tr><td colspan="2">
30+
31+
Make score(A,A) and score(A,C) be 10. It is then easy to satisfy the
32+
remaining requirements. All three pairwise comparisons score 30.0.
33+
34+
35+
</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">1)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{&quot;ACTGACTGACTG&quot;,&quot;GACTTGACCTGA&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: -4.0</pre></td></tr><tr><td><table><tr><td colspan="2">
36+
Here, there are no positions in which the letters in the two strings match each other. So we should give each exact match (each diagonal entry of the score matrix) the
37+
smallest
38+
possible score, 1. The remaining 12 pairs are each present at one position
39+
in the comparison
40+
so the best we can do is to choose those 12 scores so they add up to -4 in
41+
any manner. Now the sum of all the matrix entries is 0, and the resulting
42+
score for the one pairwise comparison is -4.
43+
</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">2)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>{&quot;ACTAGAGAC&quot;,&quot;AAAAAAAAA&quot;,&quot;TAGTCATAC&quot;,&quot;GCAGCATTC&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: 50.5</pre></td></tr><tr><td><table><tr><td colspan="2">
44+
</td></tr></table></td></tr></table></td></tr></table><p>This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. </p></body></html>

0 commit comments

Comments
 (0)