⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 3820737_ac_16ms_264k.cpp

📁 北大大牛代码 1240道题的原代码 超级权威
💻 CPP
字号:
#include <map>
#include <string>
#include <iostream>
#include <algorithm>

using namespace std;

map <string, string> h;

void init()
{
	h.clear();
	h["UUU"] = "Phe";h["UCU"] = "Ser";h["UAU"] = "Tyr";h["UGU"] = "Cys";
	h["UUC"] = "Phe";h["UCC"] = "Ser";h["UAC"] = "Tyr";h["UGC"] = "Cys";
	h["UUA"] = "Leu";h["UCA"] = "Ser";h["UAA"] = "---";h["UGA"] = "---";
	h["UUG"] = "Leu";h["UCG"] = "Ser";h["UAG"] = "---";h["UGG"] = "Trp";

	h["CUU"] = "Leu";h["CCU"] = "Pro";h["CAU"] = "His";h["CGU"] = "Arg";
	h["CUC"] = "Leu";h["CCC"] = "Pro";h["CAC"] = "His";h["CGC"] = "Arg";
	h["CUA"] = "Leu";h["CCA"] = "Pro";h["CAA"] = "Gln";h["CGA"] = "Arg";
	h["CUG"] = "Leu";h["CCG"] = "Pro";h["CAG"] = "Gln";h["CGG"] = "Arg";

	h["AUU"] = "Ile";h["ACU"] = "Thr";h["AAU"] = "Asn";h["AGU"] = "Ser";
	h["AUC"] = "Ile";h["ACC"] = "Thr";h["AAC"] = "Asn";h["AGC"] = "Ser";
	h["AUA"] = "Ile";h["ACA"] = "Thr";h["AAA"] = "Lys";h["AGA"] = "Arg";
	h["AUG"] = "Met";h["ACG"] = "Thr";h["AAG"] = "Lys";h["AGG"] = "Arg";

	h["GUU"] = "Val";h["GCU"] = "Ala";h["GAU"] = "Asp";h["GGU"] = "Gly";
	h["GUC"] = "Val";h["GCC"] = "Ala";h["GAC"] = "Asp";h["GGC"] = "Gly";
	h["GUA"] = "Val";h["GCA"] = "Ala";h["GAA"] = "Glu";h["GGA"] = "Gly";
	h["GUG"] = "Val";h["GCG"] = "Ala";h["GAG"] = "Glu";h["GGG"] = "Gly";
}

string change1(const char *str)
{
	string ret = "";
	int i;

	for (i = 0; str[i]; i++)
	{
		if (str[i] == 'A')
			ret += "U";
		else
			if (str[i] == 'U')
				ret += "A";
			else
				if (str[i] == 'C')
					ret += "G";
				else
					ret += "C";
	}
	return ret;
}

string turn(string str)
{
	string ret = "";

	for (int i = 0; i < str.length(); i++)
	{
		if (str.at(i) == 'T')
			ret += "U";
		else
			ret += str.at(i);
	}
	return ret;
}

string rev(string str)
{
	string ret = "";

	for (int i = str.length() - 1; i >= 0; i--)
	{
		ret += str.at(i);
	}
	return ret;
}

string end[] = {"UAA", "UAG", "UGA"};

int findend(string str)
{
	int i, j;

	for (i = 0; i < str.length(); i += 3)
	{
		if (i + 3 > str.length())
			break;
		for (j = 0; j < 3; j++)
		{
			if (end[j] == str.substr(i, 3))
			{
				return i;
			}
		}
	}
	return str.length();
}

int main()
{
	string str, tmp;
	int index, i;
	int id[3];
	string bit;
	string ans;

	init();
	while (true)
	{
		cin >> str;
		if (str == "*")
			break;
		str = turn(str);
		//-s
		ans = "";
		tmp = str;
		index = tmp.find("AUG");
		if (index != -1)
			tmp = tmp.substr(index + 3);
		else
			goto n1;
		if (tmp.length() == 0)
			goto n1;
		index = findend(tmp);
		if (index == 0 || index % 3 != 0 || index == tmp.length())
			goto n1;
		tmp = tmp.substr(0, index);
		while (true)
		{
			bit = tmp.substr(0, 3);
			ans += h[bit] + "-";
			if (tmp.length() == 3)
				break;
			tmp = tmp.substr(3);
		}
		cout << ans.substr(0, ans.length() - 1) << endl;
		continue;
n1:		//-s'
		ans = "";
		tmp = rev(str);
		index = tmp.find("AUG");
		if (index != -1)
			tmp = tmp.substr(index + 3);
		else
			goto n2;
		if (tmp.length() == 0)
			goto n2;
		index = findend(tmp);
		if (index == 0 || index % 3 != 0 || index == tmp.length())
			goto n2;
		tmp = tmp.substr(0, index);
		while (true)
		{
			bit = tmp.substr(0, 3);
			ans += h[bit] + "-";
			if (tmp.length() == 3)
				break;
			tmp = tmp.substr(3);
		}
		cout << ans.substr(0, ans.length() - 1) << endl;
		continue;
n2:		//-sv'
		ans = "";
		tmp = rev(change1(str.c_str()));
		index = tmp.find("AUG");
		if (index != -1)
			tmp = tmp.substr(index + 3);
		else
			goto n3;
		if (tmp.length() == 0)
			goto n3;
		index = findend(tmp);
		if (index == 0 || index % 3 != 0 || index == tmp.length())
			goto n3;
		tmp = tmp.substr(0, index);
		while (true)
		{
			bit = tmp.substr(0, 3);
			ans += h[bit] + "-";
			if (tmp.length() == 3)
				break;
			tmp = tmp.substr(3);
		}
		cout << ans.substr(0, ans.length() - 1) << endl;
		continue;
n3:		//-sv
		ans = "";
		tmp = change1(str.c_str());
		index = tmp.find("AUG");
		if (index != -1)
			tmp = tmp.substr(index + 3);
		else
			goto n4;
		if (tmp.length() == 0)
			goto n4;
		index = findend(tmp);
		if (index == 0 || index % 3 != 0 || index == tmp.length())
			goto n4;
		tmp = tmp.substr(0, index);
		while (true)
		{
			bit = tmp.substr(0, 3);
			ans += h[bit] + "-";
			if (tmp.length() == 3)
				break;
			tmp = tmp.substr(3);
		}
		cout << ans.substr(0, ans.length() - 1) << endl;
		continue;
n4:		//-end
		cout << "*** No translatable DNA found ***" << endl;
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -