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[]数组,每一列确定好字母时直接输出即可。

  • 相关阅读:
    浅谈JavaScript中this指向的⼏种情况
    JavaScript、html简单的级联操作。
    异常处理中throws和throw的区别?
    java异常处理try-catch-finally的执行过程?
    什么是内连接、外连接、交叉连接(笛卡尔积)?
    主键和外键的区别
    集合和数组的比较(为什么要引入集合)?
    Java中对比单继承与多继承的优劣,以及java的解决方案
    数据库
    数据库集中控制的优势
  • 原文地址:https://www.cnblogs.com/witharush/p/11431204.html
Copyright © 2011-2022 走看看