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

BoA、日本デビュー25周年・「BoA the LIVE」20周年を彩る…横浜でツアーファイナル

Music icon BoA has officially wrapped up her monumental concert tour with an emotional and electrifying finale in Yokohama. The performance marked a historic...

7 Reasons This Viral Kitchen Gadgets Is Worth Every Single Penny

📢 As an Amazon Associate, I earn from qualifying purchases.If you had told me six months ago that a single purchase would change how...

One reason lithium-ion batteries slowly lose capacity isn’t that the lithium disappears, it’s that a microscopic film called the solid electrolyte interphase can keep...

Lithium-ion batteries power modern life, keeping everything from handheld smartphones to long-range electric vehicles running smoothly. Yet anyone who has owned a rechargeable device...

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!