【每日一题】UVA - 1368 DNA Consensus String 字符串+贪心+阅读题

https://cn.vjudge.net/problem/UVA-1368

二维的hamming距离算法:

For binary strings a and b the Hamming distance is equal to the number of ones (population count) in a XOR b.

int hamming_distance(unsigned x, unsigned y)
{
    int dist = 0;
    unsigned  val = x ^ y;

    // Count the number of bits set
    while (val != 0)
    {
        // A bit is set, so increment the count and clear the bit
        dist++;
        val &= val - 1;
    }

    // Return the number of differing bits
    return dist;
}

 

Two example distances: 100→011has distance 3; 010→111 has distance 2

以上是题外话,

其实二进制hamming距离和这题无关啦,就是一个暴力算hamming距离,然后贪心地找

坑点:for(i,0,len)写错,应该是len-1,很难debug出来

orz 我先用set,然后发现还是要重载运算符orz 最后map暴力,还不如数组呢

#define _CRT_SECURE_NO_WARNINGS
#include<cmath>
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<stack>
#include<vector>
#include<string.h>
#include<queue>
#include<string>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
#define mod 1000000007
#define rep(i,t,n)  for(int i =(t);i<=(n);++i)
#define per(i,n,t)  for(int i =(n);i>=(t);--i)
#define mmm(a,b) memset(a,b,sizeof(a))
#define eps 1e-6
#define pb push_back
#define mp make_pair
#define x first
#define y second
const int maxn = 55;
ll n,m;

string s[maxn];
map<char, int> mmp;

int main()
{

    int t; cin >> t;
    while (t--) {
        cin >> n >> m;
        rep(i, 1, n)cin >> s[i];
        string ans ; ans.clear();
        int now = 0, mn = 0;
        rep(j, 0, m-1) {
            
            mmp.clear();
            rep(i, 1, n) {
                //cnt[s[i][j]]++;
                mmp[s[i][j]]++;

            }
            pair<char, int> temp = { 'Z',-1 };
            for (auto t : mmp)if (t.second > temp.second|| (t.second == temp.second&&t.first<temp.first))temp = t; 
            ans.push_back(temp.first);
            mn += n -temp.second;

        }
        cout << ans << endl;
        cout << mn << endl;
    }


    
    cin >> n;
    return 0;
}
/*
3
5 8
TATGATAC
TAAGCTAC
AAAGATCC
TGAGATAC
TAAGATGT
4 10
ACGTACGTAC
CCGTACGTAG
GCGTACGTAT
TCGTACGTAA
6 10
ATGTTACCAT
AAGTTACGAT
AACAAAGCAA
AAGTTACCTT
AAGTTACCAA
TACTTACCAA
*/

 

posted @ 2018-08-07 19:41  SuuTTT  阅读(152)  评论(0编辑  收藏  举报