Mammoth's Genome Decoding
The process of mammoth's genome decoding in Berland comes to its end!
One of the few remaining tasks is to restore unrecognized nucleotides in a found chain s. Each nucleotide is coded with a capital letter of English alphabet: 'A', 'C', 'G' or 'T'. Unrecognized nucleotides are coded by a question mark '?'. Thus, sis a string consisting of letters 'A', 'C', 'G', 'T' and characters '?'.
It is known that the number of nucleotides of each of the four types in the decoded genome of mammoth in Berland should be equal.
Your task is to decode the genome and replace each unrecognized nucleotide with one of the four types so that the number of nucleotides of each of the four types becomes equal.
Input
The first line contains the integer n (4 ≤ n ≤ 255) — the length of the genome.
The second line contains the string s of length n — the coded genome. It consists of characters 'A', 'C', 'G', 'T' and '?'.
Output
If it is possible to decode the genome, print it. If there are multiple answer, print any of them. If it is not possible, print three equals signs in a row: "===" (without quotes).
Examples
Input
8 AG?C??CT
Output
AGACGTCT
Input
4 AGCT
Output
AGCT
Input
6 ????G?
Output
===
Input
4 AA??
Output
===
Note
In the first example you can replace the first question mark with the letter 'A', the second question mark with the letter 'G', the third question mark with the letter 'T', then each nucleotide in the genome would be presented twice.
In the second example the genome is already decoded correctly and each nucleotide is exactly once in it.
In the third and the fourth examples it is impossible to decode the genom.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main()
{
int n, i, A, G, C, T, flag, d;
char s[260];
scanf("%d\n", &n);
gets(s);
A = G = C = T = 0;
flag = 1;
d = n / 4;//d等于每一个核苷酸的个数
for(i = 0; i < n; i++)
{
if(s[i] == 'A') A++;
if(s[i] == 'G') G++;
if(s[i] == 'C') C++;
if(s[i] == 'T') T++;
}//输入AGCT四种核苷酸的数目
if(A > d || G > d || C > d || T > d) flag = 0;
//如果有一种核苷酸数目大于d,则此组数据错误
if(A > d) A = A - d;
else A = d - A;
if(G > d) G = G - d;
else G = d - G;
if(C > d) C = C - d;
else C = d - C;
if(T > d) T = T - d;
else T = d - T;
//计算剩余未编码的各个核苷酸数目
if(n % 4 != 0) flag = 0;//如果输入的核苷酸数目不能被4整除,则也是错误数据
for(i = 0; i < n; i++)
{
if(s[i] == '?')
{
if(A > 0)
{
s[i] = 'A';
A--;
}
else if(G > 0)
{
s[i] = 'G';
G--;
}
else if(C > 0)
{
s[i] = 'C';
C--;
}
else if(T > 0)
{
s[i] = 'T';
T--;
}
//将基因链补充完整
else flag = 0;//若四种核苷酸都用完了还没补充完整,则数据错误
}
}
if(flag) puts(s);
else printf("===\n");
return 0;
}
这个代码仍可改进,每一次出现flag = 0时都应结束后面的剩余程序,直接输出结果。
可是我不会改,如果有会的小伙伴可以教教我。