題意:
給定m個長度爲n的DNA序列,求一個DNA序列到所有DNA序列的Hamming距離最小,兩個字符串的Hamming距離定義爲:字符不同的位置數
分析:
統計每一列的ACGT的數量,取最多的爲rst,然後統計Hamming距離
Code:
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define DIR 4
#define DIM 2
#define STATUS 2
#define MAXCNT 50 + 10
#define MAXN 1000 + 10
#define oo (~0u)>>1
#define INF 0x3F3F3F3F
#define REPI(i, s, e) for(int i = s; i <= e; i ++)
#define REPD(i, e, s) for(int i = e; i >= s; i --)
static const double EPS = 1e-5;
int hash[MAXCNT];
char str[MAXCNT][MAXN], rst[MAXN];
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int n, len, cas;
scanf("%d", &cas);
REPI(k, 1, cas) {
scanf("%d %d", &n, &len);
REPI(i, 0, n-1) {
scanf("%s", str[i]);
}
int ans = 0;
REPI(c, 0, len-1) {
hash['A'-'A'] = hash['C'-'A'] = 0;
hash['T'-'A'] = hash['G'-'A'] = 0;
REPI(r, 0, n-1) {
hash[str[r][c]-'A'] += 1;
}
char ch = 'A';
int max_val = hash['A'-'A'], sum = 0;
sum += hash['A'-'A'];
sum += hash['C'-'A'];
sum += hash['G'-'A'];
sum += hash['T'-'A'];
if( max_val < hash['C'-'A'] ) {
max_val = hash['C'-'A'], ch = 'C';
}
if( max_val < hash['G'-'A'] ) {
max_val = hash['G'-'A'], ch = 'G';
}
if( max_val < hash['T'-'A'] ) {
max_val = hash['T'-'A'], ch = 'T';
}
rst[c] = ch, ans += sum-hash[ch-'A'];
}
REPI(i, 0, len-1) {
printf("%c", rst[i]);
}
printf("\n%d\n", ans);
}
return 0;
}