您的位置:首页 > 其它

DNA序列(DNA Consensus String, ACM/ICPC seoul 2006, UVa 1368)

2017-08-08 15:40 543 查看

题目详情请参考原题↓:

UVa 1368

题目中的定义

Haming distance :
    两个等长字符串的Hamming distance 等于字符不同的位置个数。例如, ACGT 和 GCGA 的Hamming distance 为2 。

注意事项

输出到m个序列的Hamming distance 和最小的DNA 序列 和对应的distance 。如有多解,要求为字典序最小的解。



算法思路:

以下串为例:

TATGATAC

TAAGCTAC

AAAGATCC

TGAGATAC

TAAGATGT




注意,在每一列中,取出现次数最多的字符放入答案字符串,例如第一列中 字符T出现4次为最多。以此类推,8列共取T A A G A T A C。

故上例的最优解为TAAGATAC,

在定义常量数组是,将A C G T按字典序排列,就可以很方便的保存并输出字典序最小的字符,即保存字典序最小的答案串,解决了多解问题。

代码详情:

/*
problem sequence :Dnf序列(DNA Consensus String, ACM/ICPC seoul 2006, UVa1368)
*/
# include <cstdio>
# include <cstring>
const int maxn = 1000+10;
const int maxm = 50+5;
const char c[4] = { 'A','C','G','T' };
int x[4] = { 0 };
int m, n;

void get_maxchar(char (*a)[maxn],char (*d)) {//竖向遍历数组,将出现几率最高的字符赋给d
for (int i = 0; i <n ; i++) {
int max = 0;
for (int j = 0; j < m; j++) {
if (a[j][i] == c[0]) {
x[0]++;
}
else if (a[j][i] == c[1]) {
x[1]++;
}
else if (a[j][i] == c[2]) {
x[2]++;
}
else if (a[j][i] == c[3]) {
x[3]++;
}
for (int k = 0; k < 4; k++) {//获取字符出现最高次数
if (max < x[k])
max = x[k];
}
}
for (int k = 0; k < 4; k++) {

if (max == x[k]) {
d[i] = c[k];
break;
}
}
memset(x, 0, sizeof(x));//清空x数组
}
}
int get_ConErr(char(*a)[maxn], char *d ){
//将数组d与数组s的每行比较,如果有一个position不同,则count+1;便利m次;
int cnt = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
//d与s比较
if (a[i][j] != d[j])
cnt++;

}
}
return cnt;
}
int main() {
//freopen("data1.in", "r", stdin);
//freopen("data1.out", "w", stdout);
int T;
scanf("%d", &T);
while (T--) {

scanf("%d%d", &m, &n);
char s[maxm][maxn];
char d[maxn];
memset(s, 0, sizeof(s));
memset(d, 0, sizeof(d));
//get strings
for (int i = 0; i < m; i++) {
scanf("%s", s[i]);
}
get_maxchar(s, d);
printf("%s\n", d);
printf("%d\n", get_ConErr(s,d));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: