UVA-1368 DNA Consensus String(思路)
题目:
题意:
题目虽然比较长,但读完之后题目的思路还是比较容易想出来的。
给出m个长度为n的字符串(只包含‘A’、‘T’、‘G’、‘C’),我们的任务是得出一个字符串,要求这个字符串与给出的m个字符串的汉明距离的和最小,输出这个字符串和最小的汉明距离和。
如果有多个符合题意的字符串,就输出字典序最小的那个字符串。
思路:
1、首先给出汉明距离的定义:
汉明距离表示相同长度的字对应位不同的数量。
例如:
10100和11000的汉明距离就是2.
2、对m个字符串的每一位分别统计‘A’、‘T’、‘G’、‘C’的个数,则答案字符串这一位的字符就是m个字符串中这一位上个数最多的那个字符。这样保证答案字符串的这一位的字符和给出的所有字符串有最大的字符相同数,所以得出的汉明距离就会最小。
3、得到答案字符串之后,与每一个给出的字符串进行比较得到汉明距离的和。
代码:
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define FRE() freopen("in.txt","r",stdin)
using namespace std;
typedef long long ll;
const int maxn = 5e3+;
int mp[][];
char str[][]; int toTransform(char ch){
int res;
switch(ch){
case 'A':
res = ;
break;
case 'C':
res = ;
break;
case 'G':
res = ;
break;
case 'T':
res = ;
break;
}
return res;
} char toChar(int x){
char ch;
switch(x){
case :
ch = 'A';
break;
case :
ch = 'C';
break;
case :
ch = 'G';
break;
case :
ch = 'T';
break;
}
return ch;
} int toJudgeMaxPos(int r){
int idx = ,mmax = -inf;
for(int i = ; i<; i++){
if(mmax < mp[r][i]){
mmax = mp[r][i];
idx = i;
}
}
return idx;
} int toCountRes(char* sstr,int m,int n){
int res = ;
for(int i = ; i<n; i++){
for(int j = ; j<m; j++){
if(sstr[i] != str[j][i])
res++;
}
}
return res;
} int main(){
int T,m,n;
scanf("%d",&T);
while(T--){
scanf("%d%d",&m,&n);
for(int i = ; i<m; i++){
scanf("%s",str[i]);
} memset(mp,,sizeof(mp));
for(int i = ; i<n; i++){//统计m个字符串相同位上A、T、G、C的个数
for(int j = ; j<m; j++){
int idx = toTransform(str[j][i]);
mp[i][idx]++;
}
}
// for(int i = 0; i<m; i++){
// printf("%d %d %d %d\n",mp[i][0],mp[i][1],mp[i][2],mp[i][3]);
// }
char ans[];
for(int i = ; i<n; i++){//找出该位上相同个数最多的那个字符,并赋值给ans字符串
int pos = toJudgeMaxPos(i);
char temp = toChar(pos);
ans[i] = temp;
}
ans[n] = '\0';
// cout<<ans<<endl; int res = toCountRes(ans,m,n);//获取汉明距离
printf("%s\n%d\n",ans,res);
}
return ;
}
/*
PutIn:
3
5 8
TATGATAC
TAAGCTAC
AAAGATCC
TGAGATAC
TAAGATGT
4 10
ACGTACGTAC
CCGTACGTAG
GCGTACGTAT
TCGTACGTAA
6 10
ATGTTACCAT
AAGTTACGAT
AACAAAGCAA
AAGTTACCTT
AAGTTACCAA
TACTTACCAA
PutOut:
TAAGATAC
7
ACGTACGTAA
6
AAGTTACCAA
12
*/
UVA-1368 DNA Consensus String(思路)的更多相关文章
- uva 1368 DNA Consensus String
这道题挺简单的,刚开始理解错误,以为是从已有的字符串里面求最短的距离,后面才发现是求一个到所有字符串最小距离的字符串,因为这样的字符串可能有多个,所以最后取最小字典序的字符串. 我的思路就是求每一列每 ...
- 【每日一题】UVA - 1368 DNA Consensus String 字符串+贪心+阅读题
https://cn.vjudge.net/problem/UVA-1368 二维的hamming距离算法: For binary strings a and b the Hamming distan ...
- UVa 3602 - DNA Consensus String 水题 难度: 0
题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- 贪心水题。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 ...
- 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 ...
- 【习题 3-7 UVA - 1368 】DNA Consensus String
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举每一位字母是什么. 从小到大枚举. 然后计算每一位的总贡献是多少. 取最小的那个输出. [代码] #include <bi ...
- DNA Consensus String UVA - 1368
题目链接:https://vjudge.net/problem/UVA-1368 题意:给出一组字符串,求出一组串,使与其他不同的点的和最小 题解:这个题就是一个点一个点求,利用桶排序,求出最多点数目 ...
随机推荐
- CollapsingToolbarLayout Toolbar的title覆盖问题
CollapsingToolbarLayout 里: 1 2 app:titleEnabled="true" app:title="Hello" Toolbar ...
- codeforces round 416 div2 补题 CF 811 A B C D E
A. Vladik and Courtesy 水题略过 #include<cstdio> #include<cstdlib> #include<cmath> usi ...
- 在sql语句中使用关键字
背景 开发过程中遇到了遇到了一句sql语句一直报错,看了一下字段名和表名都对应上了,但是还是一直报错 sql语句如下: update table set using = ""hh ...
- [笔试面试题] 10-C和C++区别相关
1 C和C++有什么不同? 机制不同:C是面向过程的(但C也可以编写面向对象的程序):C++是面向对象的,提供了类.但是,C++编写面向对象的程序比C容易. 适用领域不同:C适合要求代码体积小的,效率 ...
- F - System Overload(约瑟夫环变形)
Description Recently you must have experienced that when too many people use the BBS simultaneously, ...
- pyinstaller遇到的坑
最近接了一个python的活,具体的就不展开,大概就是需要搭建一个服务器,接收客户端上传文件,调用算法模型,然后返回相应的数据.算法模块用的是tensorflow模块,里面一大堆东西,网上看了很多,最 ...
- netty 引用计数器 ,垃圾回收
netty 引用计数器 ,垃圾回收 https://blog.csdn.net/u013851082/article/details/72170065 Netty之有效规避内存泄漏 https://w ...
- 常用的几个Dos命令-持续更新中
1.服务相关 (1).查看服务 C:\Windows\system32>net start 已经启动以下 Windows 服务: (2).启动服务 C:\Windows\system32> ...
- leetcode523 Continuous Subarray Sum
思路: 令sum[p]表示p位置的前缀和.如果sum[i] % k == sum[j] % k (j - i > 1),则存在子段(i, j]的和能够整除k. 实现: class Solutio ...
- anime.js 实战:实现一个带有描边动画效果的复选框
在网页或者是APP的开发中,动画运用得当可以起到锦上添花的作用.正确使用动画,不但可以有助于用户理解交互的作用,还可以大大提高网页应用的魅力和使用体验.并且在现在的网页开发中,动画已经成为了一个设计的 ...