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; }