建立 A-T、G-C 的互补映射,再 translate 得到配对碱基链。
OJ: noi_openjudge
题目 ID: ch0107-07
难度:入门
标签:字符串映射python
日期: 2026-07-30 23:01
题意
输出 DNA 单链的互补碱基链,其中 A 与 T 配对,G 与 C 配对。
思路
用 str.maketrans 建立四个字符的替换表,translate 一次转换整条链。
代码
Python代码
python
base_pair = str.maketrans({"A": "T", "T": "A", "G": "C", "C": "G"})
print(input().strip().translate(base_pair))C++代码
cpp
#include <cstdio>
#include <cstring>
#define maxn 500
int n;
char str[maxn];
char _map[maxn];
int main(){
_map['A'] = 'T';
_map['T'] = 'A';
_map['G'] = 'C';
_map['C'] = 'G';
scanf("%s",str+1);
int len = strlen(str+1);
int i;
for (i=1;i<=len;i++){
printf("%c",_map[ str[i]] );
}
return 0;
}复杂度
时间复杂度和输出空间均为
总结
固定的一对一字符替换可用翻译表清楚表示。