Codeforces Round #387 (Div. 2), problem: (B) Mammoth’s Genome Decoding Solution in C/C++

Codeforces Round #387 (Div. 2), problem: (B) Mammoth’s Genome Decoding Solution in C/C++

#include <stdio.h>

char cc[] = { 'A', 'C', 'G', 'T' };
int cnt[4];

void incr(char c) {
	int i;

	for (i = 0; i < 4; i++)
		if (cc[i] == c) {
			cnt[i]++;
			return;
		}
}

int main() {
	int i, j, n, yes;
	static char s[512];

	scanf("%d", &n);
	scanf("%s", s);
	yes = 1;
	if (n % 4 != 0)
		yes = 0;
	for (i = 0; i < n; i++)
		if (s[i] != '?')
			incr(s[i]);
	for (i = 0; i < n; i++) {
		if (cnt[i] > n / 4) {
			yes = 0;
			break;
		}
		cnt[i] = n / 4 - cnt[i];
	}
	for (i = j = 0; i < n; i++) {
		while (cnt[j] == 0)
			j++;
		if (s[i] == '?') {
			cnt[j]--;
			s[i] = cc[j];
		}
	}
	printf("%s\n", yes ? s : "===");
	return 0;
}

Codeforces Round #387 (Div. 2), problem: (B) Mammoth’s Genome Decoding Solution in C/C++

#include <stdio.h>

char cc[] = { 'A', 'C', 'G', 'T' };
int cnt[4];

void incr(char c) {
	int i;

	for (i = 0; i < 4; i++)
		if (cc[i] == c) {
			cnt[i]++;
			return;
		}
}

int main() {
	int i, j, n, yes;
	static char s[512];

	scanf("%d", &n);
	scanf("%s", s);
	yes = 1;
	if (n % 4 != 0)
		yes = 0;
	for (i = 0; i < n; i++)
		if (s[i] != '?')
			incr(s[i]);
	for (i = 0; i < n; i++) {
		if (cnt[i] > n / 4) {
			yes = 0;
			break;
		}
		cnt[i] = n / 4 - cnt[i];
	}
	for (i = j = 0; i < n; i++) {
		while (cnt[j] == 0)
			j++;
		if (s[i] == '?') {
			cnt[j]--;
			s[i] = cc[j];
		}
	}
	printf("%s\n", yes ? s : "===");
	return 0;
}

More from author

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Related posts

Advertismentspot_img

Latest posts

Trump lays out the pomp for Xi but says little about the significant tensions in US-China relations

A Grand Diplomatic Spectacle Amid Underlying Geopolitics In a high-profile diplomatic engagement that blended elaborate ceremonial fanfare with cautious statecraft, Donald Trump welcomed Chinese President...

Everything You Need to Know About Viral Kitchen Gadgets Right Now

📢 As an Amazon Associate, I earn from qualifying purchases.There comes a point when you realise that tolerating a frustrating situation is costing you...

Ideas on modernizing the open-source desktop

For decades, the Linux community has delivered unparalleled stability, modularity, and power. However, user experience and interface design often lag behind proprietary alternatives. Veteran...

Want to stay up to date with the latest news?

We would love to hear from you! Please fill in your details and we will stay in touch. It's that simple!