LA_3602 DNA Consensus String ( hash, 貪心 )

9 篇文章 0 订阅
8 篇文章 0 订阅

題意:
給定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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值