uva 1368 DNA Consensus String

点击打开链接uva 1368

思路:暴力
分析:
1 给定m个长度均为n的DNA序列,求一个DNA使其所有的序列的
Hamming distances最小,如果有多个解输出字典序最小的序列
2 m最大为50,n最大为1000,很明显的暴力题目,没什么解题的方法就是暴力

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int N = 100;
const int maxn = 1010;
const char ch[4] = {'A' , 'C' , 'G' , 'T'};
char str[N][maxn];

//得到下标
int getIndex(char c){
    for(int i = 0 ; i < 4 ; i++)
       if(c == ch[i])
          return i;
}

//得到最大的值的下标
int getMax(int *num){
    int pos = 0;
    for(int i = 1 ; i < 4 ; i++)
       if(num[i] > num[pos])
         pos = i;
    return pos;
}

void solve(int m , int n){
    int num[4];
    int ans = 0;
    char ansStr[maxn];
    memset(ansStr , '\0' , sizeof(ansStr));
    for(int i = 0 ; i < n ; i++){
       memset(num , 0 , sizeof(num));
       for(int j = 0 ; j < m ; j++)
          num[getIndex(str[j][i])]++;
       int maxIndex = getMax(num);
       ansStr[i] = ch[maxIndex];
       ans += m-(num[maxIndex]);
    }
    printf("%s\n%d\n" , ansStr , ans);
}

int main(){
    int Case , m , n;
    scanf("%d" , &Case);
    while(Case--){
        scanf("%d%d" , &m , &n);
        for(int i = 0 ; i < m ; i++)
           scanf("%s" , str[i]);
        solve(m , n);
    }
    return 0;
}
时间: 2025-01-02 18:53:22

uva 1368 DNA Consensus String的相关文章

UVa 10739 String to Palindrome (DP)

10739 - String to Palindrome Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=1680 思路:对于每个区间[i, j]: 若str[i] == str[j],dp[i][j] = dp[i + 1][j - 1]; 若str[i]

算法:uva 1351 String Compression(字符串区间dp)

题目大意 给一个字符串,可以把连续相同的部分进行缩写成k(S)的形式,S是一个字符串,k表示 有连续相同的S 例如,abgogogogo,可以缩写成ab4(go). 还可以嵌套缩写,比如 "nowletsgogogoletsgogogo", 缩写成"now2(lets3(go))" 思路 一道区间dp,但是这题并 不好想 f(i, j)表示字符串的i~j位的最小位数 那么 f(i, j) = min{  min{ f(i,k)+f(k+1, j), i<=k&

算法题:UVA 11258 String Partition(dp)

Problem F - String Partition John was absurdly busy for preparing a programming contest recently. He wanted to create a ridiculously easy problem for the contest. His problem was not only easy, but also boring: Given a list of non-negative integers,

UVa 10602

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1543 类型:贪心 原题: Company Macrohard has released it's new version of editor Nottoobad, which can understand a few voice commands.

UVa 10183 How Many Fibs? (统计斐波那契数个数&amp;amp;高精度)

10183 - How Many Fibs? Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1124 Recall the definition of the Fibonacci numbers:    f1 := 1    f2 := 2    fn :

UVA 之401 - Palindromes

A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string "ABCDEDCBA" is a palindrome because it is the same when the string is read from left to right as when the string is read from ri

UVA之409 - Excuses, Excuses!

 Excuses, Excuses!  Judge Ito is having a problem with people subpoenaed for jury duty giving rather lame excuses in order to avoid serving. In order to reduce the amount of time required listening to goofy excuses, Judge Ito has asked that you write

UVA之12124 - Assemble

[题目] Problem A - Assemble Time limit: 2 seconds Recently your team noticed that the computer you use to practice for programming contests is not good enough anymore. Therefore, you decide to buy a new computer. To make the ideal computer for your nee

UVA之11462 - Age Sort

[题目] You are given the ages (in years) of all people of a country with at least 1 year of age. You know that no individual in that country lives for 100 or more years. Now, you are given a very simple task of sorting all the ages in ascending order.