题目链接:https://vjudge.net/problem/UVA-1368
字符串模拟,可以从字符矩阵中一列一列的查找,列出每列中出现次数最多的字母,拼合起来便是距离最小的DNA。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
//A 0 C 1 G 2 T 3
int main() {
freopen("i.txt","r",stdin);
int n;
int arr[1010][4];
char dna[100][1010];
int l[1010];
int t[100][1010];
scanf("%d",&n);
while(n-->0) {
memset(t,0,sizeof(t));
memset(l,0,sizeof(l));
memset(arr,0,sizeof(arr));
int x,y;
scanf("%d%d",&x,&y);
for(int i = 0; i < x; i++)
scanf("%s",dna[i]);
for(int i = 0; i < y; i++) {
for(int j = 0; j < x; j++) {
if(dna[j][i]=='A') {
arr[i][0]++;
t[j][i] = 0;
}
else if(dna[j][i]=='C') {
arr[i][1]++;
t[j][i] = 1;
}
else if(dna[j][i]=='G') {
arr[i][2]++;
t[j][i] = 2;
}
else if(dna[j][i]=='T') {
arr[i][3]++;
t[j][i] = 3;
}
}
}
int max = 0, index = 0;
for(int i = 0; i < y; i++) {
max = arr[i][0], index = 0;
for(int j = 0; j < 4; j++) {
if(arr[i][j] > max) {
max = arr[i][j];
index = j;
}
else if(arr[i][j]==max) {
if(j<l[i]) index = j;
}
}
l[i] = index;
}
for(int i = 0; i < y; i++) {
if(l[i]==0) putchar('A');
else if(l[i]==1) putchar('C');
else if(l[i]==2) putchar('G');
else if(l[i]==3) putchar('T');
}
int sum = 0;
for(int i = 0; i < x; i++) {
for(int j = 0; j < y; j++) {
if(t[i][j]!=l[j]) sum++;
}
}
printf("\n%d\n",sum);
}
return 0;
}