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 题意:给出一组字符串,求出一组串,使与其他不同的点的和最小 题解:这个题就是一个点一个点求,利用桶排序,求出最多点数目 ...
随机推荐
- commons.fileupload 文件上传
编辑jsp页面获取文件 <html> <head> <base href="<%=basePath%>"> <title> ...
- YTU 2642: 填空题:类模板---求数组的最大值
2642: 填空题:类模板---求数组的最大值 时间限制: 1 Sec 内存限制: 128 MB 提交: 646 解决: 446 题目描述 类模板---求数组的最大值 找出一个数组中的元 ...
- nestedScrollview 嵌套使用recyclerview判断滑动到底部 (嵌套滑动导致 不能使用recyclerview的onscrolled监听)
NestedScrollView scroller = (NestedScrollView) findViewById(R.id.myScroll); if (scroller != null) { ...
- codeforces 965E Trie+multiset
E. Short Code time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 【USACO07FEB】 Cow Relays
[题目链接] 点击打开链接 [算法] 朴素算法,就是跑N-1遍floyd 而满分算法就是通过矩阵快速幂加速这个过程 [代码] ...
- 0627-TP整理三(对表的操作,数据的显示)
一.对表的操作 直接sql语句:(query/execute) 1.查询: 查询所有:M('表名')->select(); 查询一条数据:M('表名')->find(); 条件查询: 动态 ...
- bzoj 1026: [SCOI2009]windy数【数位dp】
忘记limit不能记WA了一发-- 典型数位dp,变成work(r)-work(l-1),然后dfs的时候记录w当前位置,la上一个数选的什么,lm当前位是否有上限,ok当前位是否可以不考虑差大于等于 ...
- C基础-对malloc的使用与理解
一.malloc函数分析 1.函数原型 void * malloc(size_t size); 2.Function(功能) Allocates a block of size bytes of m ...
- CF482C Game with Strings
题意 你和你的朋友玩一个游戏,游戏规则如下. 你的朋友创造 n 个长度均为 m 的不相同的字符串,然后他随机地选择其中一个.他选择这些字符串的概率是相等的,也就是说,他选择 n 个字符串中的每一个的概 ...
- SparkContext, map, flatMap, zip以及例程wordcount
SparkContext 通常作为入口函数,可以创建并返回一个RDD. 如把Spark集群当作服务端那Spark Driver就是客户端,SparkContext则是客户端的核心: 如注释所说 Spa ...