UVa 1368 - DNA Consensus String(贪心)

来源:互联网 发布:linux bash profile 编辑:程序博客网 时间:2024/05/16 18:30

题意

找出一个字符串,使得它的Hamming Distance最小。

思路

对每个位置进行贪心,选出现次数最多的字母。

代码

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <string>
  4. #include <cstring>
  5. #define LL long long
  6. #define lowbit(x) ((x) & (-x))
  7. const int MAXN = 1e3 + 5;
  8. const int INF = 0x3f3f3f3f;
  9. using namespace std;
  10. struct SCAN
  11. {
  12. char c;
  13. int num;
  14. }scan;
  15. char str[55][MAXN];
  16. int num[30];
  17. int main()
  18. {
  19. //freopen("input.txt", "r", stdin);
  20. int n, i, j, T, leng;
  21. scanf("%d", &T);
  22. while (T--)
  23. {
  24. int nerr = 0;
  25. scanf("%d%d%*c", &n, &leng);
  26. string ans;
  27. for (i = 0; i < n; i++)
  28. scanf("%s", str[i]);
  29. for (i = 0; i < leng; i++)
  30. {
  31. memset(num, 0, sizeof num);
  32. for (j = 0; j < n; j++)
  33. num[str[j][i] - 'A']++;
  34. scan.c = 'A', scan.num = num[0];
  35. for (j = 1; j < 30; j++)
  36. if (num[j] > scan.num)
  37. scan.c = j + 'A', scan.num = num[j];
  38. ans += scan.c;
  39. for (j = 0; j < 30; j++)
  40. if (j + 'A' != scan.c)
  41. nerr += num[j];
  42. }
  43. cout << ans << endl;
  44. cout << nerr << endl;
  45. }
  46. return 0;
  47. }
0 0
原创粉丝点击