zoukankan      html  css  js  c++  java
  • UVa1368 DNA序列(DNA Consensus String)

    思路:

      比较同一列A,C,G,T四个字母出现的个数,找到出现数最大的字母即答案,一样的话优先'A'(按字典顺序进行判断就可以得到字典序最小的解)

    AC代码

     1 #include<stdio.h>
     2 #include<string.h>
     3 
     4 char str[55][1005];
     5 char ans[1005];
     6 
     7 int findMax(int a, int b, int c, int d)
     8 { 
     9     int tmp[4] = {a, b, c, d};
    10     int max = 0;
    11     for(int i = 0; i < 4; i++)
    12     {
    13         if(max < tmp[i])
    14             max = tmp[i];
    15     }
    16     return max;
    17     
    18 }
    19 int main()
    20 {
    21     int T, m, n, count;
    22     scanf("%d", &T);
    23     while(T--)
    24     {
    25         count = 0;
    26         scanf("%d %d", &m, &n);
    27         getchar();    //吃掉换行符
    28         for(int i = 0; i < m; i++)
    29         {
    30             for(int j = 0; j < n; j++)
    31             {
    32                 scanf("%c", &str[i][j]);
    33             }
    34             getchar();    //吃掉换行符
    35         }
    36         //统计每一列字符出现的字数,找到最大的,若一样优先'A'
    37         for(int i = 0; i < n; i++)
    38         {
    39             int a = 0, c = 0, g = 0, t = 0;
    40             for(int j = 0; j < m; j++)
    41             {
    42                 if(str[j][i] == 'A')
    43                     a++;
    44                 if(str[j][i] == 'C')
    45                     c++;
    46                 if(str[j][i] == 'G')
    47                     g++;
    48                 if(str[j][i] == 'T')
    49                     t++;
    50             }
    51             int max = findMax(a, c, g, t);
    52             if(a == max)
    53             {
    54                 ans[i] = 'A';
    55                 continue;
    56             }    
    57             if(c == max)
    58             {
    59                 ans[i] = 'C';
    60                 continue;
    61             }    
    62             if(g == max)
    63             {
    64                 ans[i] = 'G';
    65                 continue;
    66             }
    67             if(t == max)
    68             {
    69                 ans[i] = 'T';
    70                 continue;
    71             }    
    72         }
    73         //计算总Hamming
    74         for(int i = 0; i < m; i++)
    75         {
    76             for(int j = 0; j < n; j++)
    77             {
    78                 if(ans[j] != str[i][j])
    79                     count++;    
    80             }    
    81         } 
    82         for(int i = 0; i < n; i++)
    83             printf("%c", ans[i]);
    84         printf("
    ");
    85         printf("%d
    ", count);
    86     }    
    87 }

    PS:

      其实没有必要开ans[]数组,每一列确定好字母时直接输出即可。

  • 相关阅读:
    【大数据】Hadoop的伪分布式安装
    【运维】什么是EPEL?
    【架构】RESTful的架构思想
    【python】有关python的异或操作的分析
    【Python】有关os.path.dirname(__file__)的注意事项
    Python中字符串前添加r ,b, u, f前缀的含义
    【Confluence】在CentOS7上的安装小记(下)
    Redis应用场景
    spring的context:exclude-filter 与 context:include-filter
    Spring的<context:annotation-config>和<annotation-driven>
  • 原文地址:https://www.cnblogs.com/witharush/p/11431204.html
Copyright © 2011-2022 走看看