UVa 1368

找一个跟所给的序列的总Hamming距离最小的序列,输出字典序最小的解

Hamming距离就是两个序列有几个位置的不同字符

 1 #include <iostream>
 2 using namespace std;
 3 int t,n,m,ans1,cnt,tmp1,tmp2,tmp3,tmp4;
 4 char s[55][1005],ans2[1005];
 5 void judge()
 6 {
 7     if(tmp1>=tmp2&&tmp1>=tmp3&&tmp1>=tmp4) 
 8     {
 9         ans2[cnt++]='A';
10         ans1+=tmp2+tmp3+tmp4;
11     } 
12     else if(tmp2>=tmp1&&tmp2>=tmp3&&tmp2>=tmp4)
13     {
14         ans2[cnt++]='C';
15         ans1+=tmp1+tmp3+tmp4;
16     }
17     else if(tmp3>=tmp2&&tmp3>=tmp1&&tmp3>=tmp4)
18     {
19         ans2[cnt++]='G';
20         ans1+=tmp1+tmp2+tmp4;
21     }
22     else
23     {
24         ans2[cnt++]='T';
25         ans1+=tmp1+tmp2+tmp3;
26     }
27 }
28 void fuc()
29 {
30     ans1=cnt=0;
31     for(int j=0;j<m;j++)
32     {
33         tmp1=tmp2=tmp3=tmp4=0;
34         for(int i=0;i<n;i++)
35         {
36             switch(s[i][j])
37             {
38                 case 'A': tmp1++;break;
39                 case 'C': tmp2++;break;
40                 case 'G': tmp3++;break;
41                 case 'T': tmp4++;break;
42             }
43         }    
44         judge();
45     }
46     ans2[cnt]='';
47 }
48 int main()
49 {
50     cin>>t;
51     while(t--)
52     {
53         cin>>n>>m;
54         for(int i=0;i<n;i++) cin>>s[i];
55         fuc();
56         cout<<ans2<<endl;
57         cout<<ans1<<endl;
58     }
59 }
我自倾杯,君且随意
原文地址:https://www.cnblogs.com/nicetomeetu/p/5285826.html