B. Mammoth’s Genome Decoding
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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, s is 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.
题意:给定由四个大写字母ACGT和一个问号’?’组成的字符串,问号可以替换成四个字母中的任意一个,最终需要四个字母出现次数相同。输出替换后的字符串。
思路:简单模拟一下,考虑点边界就没问题。
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<string>
using namespace std;
const int maxn=256;
int change(char c)
{
if(c=='?')
return 0;
if(c=='A')
return 1;
if(c=='C')
return 2;
if(c=='G')
return 3;
return 4;
}
char AC(int x)
{
if(x==1)
return 'A';
if(x==2)
return 'C';
if(x==3)
return 'G';
return 'T';
}
int main()
{
char str[maxn];
int N;
scanf("%d",&N);
scanf("%s",str);
int num[5]= {0};
int max_num=0;
for(int i=0; i<N; i++)
num[change(str[i])]++;
for(int i=1; i<=4; i++)
max_num=max(max_num,num[i]);
for(int i=1; i<=4; i++)
num[0]=num[0]-(max_num-num[i]);
if(num[0]<0||num[0]%4!=0)
{
printf("===\n");
return 0;
}
max_num+=num[0]/4;
// printf("%d***\n",max_num);
//printf("%s***",str);
for(int i=0; i<N; i++)
{
if(str[i]=='?')
{
for(int j=1; j<=4; j++)
{
if(num[j]<max_num)
{
printf("%c",AC(j));
num[j]++;
break;
}
}
}
else
printf("%c",str[i]);
}
return 0;
}