1368 - DNA Consensus String

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
struct cell
{
	char ch;
	int n;
	cell(char c ='-',int t=0):ch(c),n(t){}
	bool operator<(const cell & cl)const{
		if(n>cl.n)return true;
		else if(n == cl.n && ch < cl.ch)return true;
		else return false;
	}
};
int ch2int(char c){
	switch(c){
		case 'A':
		return 0;
		case 'G':
		return 1;
		case 'C':
		return 2;
		case 'T':
		return 3;
	}
	return -1;
}
int main()
{
	int t;
	cin>>t;
	for(int k=0;k<t;k++){
		int n,m;
		cin>>n>>m;
		string dna;
		vector<string> col(m);
		for(int i=0;i<m;i++){
			col[i].resize(n);
		}
		for(int i=0;i<n;i++){
			cin>>dna;
			for(int j=0;j<m;j++){
				col[j][i]=dna[j];
			}
		}
		string res;
		int hamming = 0;
		for(int i=0;i<m;i++){
			vector<cell> cnt(4);
			cnt[0]=cell('A');cnt[1]=cell('G');cnt[2]=cell('C');cnt[3]=cell('T');
			for(int j=0;j<n;j++){
				cnt[ch2int(col[i][j])].n++;
			}
			sort(cnt.begin(),cnt.end());
			res +=cnt[0].ch;
			for(int j=0;j<n;j++){
				if(res[i] != col[i][j])
					hamming++;
			}
		}
		cout<<res<<endl<<hamming<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值