uvalive 3602 DNA Consensus String
https://vjudge.net/problem/UVALive-3602
题意:
给定m个长度均为n的DNA序列,求一个DNA序列,使得它到所有的DNA序列的汉明距离最短,若有多个解则输出字典序最小的解。
ps:汉明距离指的是两个等长字符串中字符不同的位置的个数。
思路:
贪心原则,记录每一个位置上哪个字母出现的次数最多,如果有相同的就取字典序最小的那个,然后放进答案就可以了。
代码:
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
using namespace std; map<char,int> mmp[]; struct node
{
char ch;
int sum;
} c[]; bool cmp(node aa,node bb)
{
if (aa.sum == bb.sum) return aa.ch < bb.ch; return aa.sum > bb.sum;
} int main()
{
int t; cin >> t; while (t--)
{
int m,n; cin >> m >> n; for (int i = ;i < n;i++) mmp[i].clear(); for (int i = ;i < m;i++)
{
string a; cin >> a; for (int j = ;j < n;j++)
mmp[j][a[j]]++;
} string s; int ans = ; for (int i = ;i < n;i++)
{
int cnt = ; if (mmp[i]['A'])
{
c[cnt].ch = 'A';
c[cnt].sum = mmp[i]['A'];
cnt++;
} if (mmp[i]['C'])
{
c[cnt].ch = 'C';
c[cnt].sum = mmp[i]['C'];
cnt++;
} if (mmp[i]['G'])
{
c[cnt].ch = 'G';
c[cnt].sum = mmp[i]['G'];
cnt++;
} if (mmp[i]['T'])
{
c[cnt].ch = 'T';
c[cnt].sum = mmp[i]['T'];
cnt++;
} sort(c,c+cnt,cmp); s.push_back(c[].ch); int tmp = ; for (int j = ;j < cnt;j++)
tmp += c[j].sum; ans += (tmp - c[].sum);
} cout << s << endl << ans << endl;
} return ;
}
uvalive 3602 DNA Consensus String的更多相关文章
- 贪心水题。UVA 11636 Hello World,LA 3602 DNA Consensus String,UVA 10970 Big Chocolate,UVA 10340 All in All,UVA 11039 Building Designing
UVA 11636 Hello World 二的幂答案就是二进制长度减1,不是二的幂答案就是是二进制长度. #include<cstdio> int main() { ; ){ ; ) r ...
- UVa 3602 - DNA Consensus String 水题 难度: 0
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- LA 3602 DNA Consensus String (暴力枚举)
题意:给定m个长度为n的DNA序列,求一个最短的DNA序列,使得总Hamming距离最小. Hamming距离等于字符不同的位置个数. 析:看到这个题,我的第一感觉是算时间复杂度,好小,没事,完全可以 ...
- LA 3602 - DNA Consensus String 枚举
原题地址:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- LA 3602 DNA Consensus String
最近审题老是一错再错,Orz 题目中说求一个Hamming值总和最小的字符串,而不是从所给字符中找一个最小的 这样的话,我们逐列处理,所求字符串当前位置的字符应该是该列中出现次数最多其次ASCII值最 ...
- uva1368 DNA Consensus String
<tex2html_verbatim_mark> Figure 1. DNA (Deoxyribonucleic Acid) is the molecule which contains ...
- DNA Consensus String
题目(中英对照): DNA (Deoxyribonucleic Acid) is the molecule which contains the genetic instructions. It co ...
- 紫书第三章训练1 E - DNA Consensus String
DNA (Deoxyribonucleic Acid) is the molecule which contains the genetic instructions. It consists of ...
- uva 1368 DNA Consensus String
这道题挺简单的,刚开始理解错误,以为是从已有的字符串里面求最短的距离,后面才发现是求一个到所有字符串最小距离的字符串,因为这样的字符串可能有多个,所以最后取最小字典序的字符串. 我的思路就是求每一列每 ...
随机推荐
- IOS开发之XCode学习014:警告对话框和等待提示器
此文学习来源为:http://study.163.com/course/introduction/1002858003.htm 此工程文件实现功能: 1.警告对话框和等待提示器的概念 2.警告对话框 ...
- vuex的学习笔记
什么是Vuex? vuex是一个专门为vue.js设计的集中式状态管理架构.状态?我把它理解为在data中的属性需要共享给其他vue组件使用的部分,就叫做状态.简单的说就是data中需要共用的属性. ...
- iOS开发-LayoutGuide(从top/bottom LayoutGuide到Safe Area)
iOS7 topLayoutGuide/bottomLayoutGuide 创建一个叫做LayoutGuideStudy的工程,我们打开看一下Main.storyboard: storyboard-t ...
- ASCII十进制转字符串的方法
/// <summary> /// ASCII转字符串 /// </summary> /// <param name="asciiCode">A ...
- [poj-2985]The k-th Largest Group_Treap+并查集
The k-th Largest Group poj-2985 题目大意:给你n只猫,有两种操作:1.将两只猫所在的小组合并.2.查询小组数第k大的小组的猫数. 注释:1<=n,m<=20 ...
- linux小白成长之路2————yum安装火狐浏览器
1.使用终端: CentOS左上角:应用程序>收藏>终端  2.使用root权限安装软件: 输入:sudo -i 输入密码(上篇安装linux虚拟机时设定的登录密码,也是超级管理员密码) ...
- 云计算之路-阿里云上-容器难容:容器服务故障以及自建 docker swarm 集群故障
3月21日,由于使用阿里云服务器自建 docker swarm 集群的不稳定,我们将自建 docker swarm 集群上的所有应用切换阿里云容器服务 swarm 版(非swarm mode). 3月 ...
- hackme.inndy.tw - pyyy - Writeup
hackme.inndy.tw - pyyy - Writeup 0x01 反编译 1.第一次尝试的时候我直接在线反编译,部分结果如下. for (i, f) in enumerate(F): n = ...
- UserControl 用户定义组件
<pages> <namespaces> <add namespace="System.Web.Optimization" /> </na ...
- python实现维吉尼亚解密
# -*-coding:UTF-8-*- from sys import stdout miwen = "KCCPKBGUFDPHQTYAVINRRTMVGRKDNBVFDETDGILTXR ...