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

Is Viral Kitchen Gadgets Worth Your Money Here Is Our Full Review

📢 As an Amazon Associate, I earn from qualifying purchases.I stumbled across this while desperately trying to fix a recurring problem in my routine....

Rob Schneider leans on faith, holds hope for reconciliation with estranged daughter Elle King

Actor and comedian Rob Schneider is opening up about his spiritual journey, expressing profound hope for a future reconciliation with his estranged daughter, singer-songwriter...

Stop Buying Cheap Alternatives This Viral Kitchen Gadgets Is the Real Deal

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

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!