紫书——DNA Consensus String UVA - 1368

题解:

又是难在读题目的问题,这道题的想法是这样的。

有m个字符串,然后输出所有字符串每列字符数最多的,其他不同的要加起来。如果一列中'A'和'T'相同,则输出字典序最小的'A'

代码如下:注意字典序

#include <bits/stdc++.h>
using namespace std;

#define inf 0x3f3f3f3f

int n,m;
string str[60];
map<char,int> num;	//保存每列最多的字母 

void init(){	//每次的初始化 
	num['A'] = 0;
	num['C'] = 0;
	num['G'] = 0;
	num['T'] = 0;
}

int main() {
	//freopen("in.txt","r",stdin);

	int t;
	scanf("%d",&t);
	while(t--){
		int sum = 0;	//保存总的次数 
		scanf("%d%d",&m,&n);
		for(int i = 0; i < m; i++){
			cin >> str[i];
		}
		
		for(int i = 0; i < n; i++){
			init();
			for(int j = 0; j < m; j++){
				num[str[j][i]]++;
			}
			char tmp;	//该行最多次数的字母
			int ci = 0;
			for(map<char,int>::iterator it = num.begin();it != num.end(); it++){
				if(it->second > ci){
					sum += ci;
					tmp = it->first;
					ci = it->second;
				}else sum += it->second;
			}
			printf("%c",tmp);
		}	
		printf("\n%d\n",sum);
	}
		
	return 0;
}

猜你喜欢

转载自blog.csdn.net/a673953508/article/details/80188511