习题3-7 DNA序列(DNA Consensus String,ACM/ICPC Seoul 2006,UVa1368)

原题链接:https://vjudge.net/problem/UVA-1368
分类:字符串
备注:水题
思路:每列求出出现最多的,如果有次数相同的按字典序优先。
代码如下:

#include<stdio.h>
#include<string.h>
int T, m, n, cnt[4][1005];//ACGT的顺序
char read()
{
	char ch = getchar();
	while (ch == ' ' || ch == '\n')ch = getchar();
	return ch;
}
int main(void)
{
	scanf("%d", &T);
	while (T--)
	{
		scanf("%d%d", &m, &n);
		memset(cnt, 0, sizeof(cnt));
		for(int i=1;i<=m;i++)
			for (int j = 1; j <= n; j++)
			{
				char ch = read();
				if (ch == 'A')cnt[0][j]++;
				else if (ch == 'C')cnt[1][j]++;
				else if (ch == 'G')cnt[2][j]++;
				else if (ch == 'T')cnt[3][j]++;
			}
		int ans = m * n;
		for (int i = 1; i <= n; i++)
		{
			int temp = 0;
			for (int j = 1; j <= 3; j++)if (cnt[temp][i] < cnt[j][i])temp = j;
			if (temp == 0)printf("A");
			else if (temp == 1)printf("C");
			else if (temp == 2)printf("G");
			else if (temp == 3)printf("T");
			ans -= cnt[temp][i];
		}
		printf("\n%d\n", ans);
	}
	return 0;
}
发布了22 篇原创文章 · 获赞 23 · 访问量 508

猜你喜欢

转载自blog.csdn.net/TK_wang_/article/details/104377314