Test instructions: Given a sequence of DNA of length n of M, a shortest sequence of DNA is obtained, which minimizes the total hamming distance.
Hamming the distance is equal to the number of different positions of characters.
Analysis: See this problem, my first feeling is to calculate the time complexity, good small, nothing, completely can violence, as long as the same position on each string,
The choice appears most, if has the same choice ASIIC code small (because requires the dictionary order small). Then record the maximum character and Hamming distance.
The code is as follows:
#include <iostream> #include <cstdio> #include <algorithm> #include <queue> #include <vector >using namespace Std;const int maxn = n + 10;char S[55][maxn];char ans[maxn];int main () {int n, T, M, d, MM; Cin >> T; while (t--) {scanf ("%d%d", &m, &n); for (int i = 0; i < m; ++i) scanf ("%s", S[i]); d = 0; for (int i = 0; i < n; ++i) {int cnta = 0, Cntc = 0, Cntg = 0, Cntt = 0; MM = 0; for (int j = 0; j < m; ++j) {if (' A ' = = S[j][i]) ++cnta; else if (' C ' = = S[j][i]) ++cntc; else if (' G ' = = S[j][i]) ++cntg; else if (' T ' = = S[j][i]) ++cntt; } mm = max (mm, cnta); MM = max (mm, CNTC);//Find the maximum number of MM = max (mm, CNTG); MM = max (mm, Cntt); if (mm = = Cnta) {Ans[i] = ' A '; D + = CNTC; D + = CNTG; D + = Cntt; }//different positions Plus and else if (mm = = CNTC) {Ans[i] = ' C '; D + = Cnta; D + = CNTG; D + = Cntt; } else if (mm = = CNTG) {Ans[i] = ' G '; D + = CNTC; D + = Cnta; D + = Cntt; } else if (mm = = Cntt) {Ans[i] = ' T '; D + = CNTC; D + = CNTG; D + = Cnta; }} Ans[n] = ' + ';//plus Terminator printf ("%s\n%d\n", ans, D); } return 0;}
LA 3602 DNA Consensus String (brute Force enumeration)