forked from kamyu104/AOAPCII-BeginingAlgorithmContests2nd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUVa1368.cpp
71 lines (59 loc) · 1.61 KB
/
UVa1368.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) 2015 kamyu. All rights reserved.
/*
* UVa1368 - DNA Consensus String
* http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4114
*
* Time : O(m * n)
* Space : O(1)
*
*/
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::unordered_map;
void ConsensusString(const vector<string>& DNAs,
string* consensus_string,
int* error_count) {
for (int j = 0; j < DNAs[0].length(); ++j) {
// Count.
unordered_map<char, int> count;
for (int i = 0; i < DNAs.size(); ++i) {
++count[DNAs[i][j]];
}
// Find smallest consensus nucleotide.
char consensus = 'A';
// Be aware of the order.
for (const auto& nucleotide : {'A', 'C', 'G', 'T'}) {
if (count[nucleotide] > count[consensus]) {
consensus = nucleotide;
}
}
// Update info.
(*consensus_string)[j] = consensus;
*error_count += DNAs.size() - count[consensus];
}
}
int main() {
int T;
cin >> T;
while (T--) {
int m, n;
cin >> m >> n;
vector<string> DNAs(m);
for (int i = 0; i < DNAs.size(); ++i) {
cin >> DNAs[i];
}
string consensus_string(n, 'A');
int error_count = 0;
ConsensusString(DNAs, &consensus_string, &error_count);
cout << consensus_string << endl;
cout << error_count << endl;
}
return 0;
}