| 22 | } |
| 23 | |
| 24 | std::string decode(const std::string& dna) { |
| 25 | if (dna.size() % 4 != 0) throw std::runtime_error("Invalid DNA length"); |
| 26 | std::string result; |
| 27 | result.reserve(dna.size() / 4); |
| 28 | for (size_t i = 0; i < dna.size(); i += 4) { |
| 29 | unsigned char c = 0; |
| 30 | for (int j = 0; j < 4; j++) { |
| 31 | auto it = BASE_VAL.find(std::toupper(dna[i+j])); |
| 32 | if (it == BASE_VAL.end()) throw std::runtime_error("Invalid base: " + std::string(1, dna[i+j])); |
| 33 | c = (c << 2) | it->second; |
| 34 | } |
| 35 | result += c; |
| 36 | } |
| 37 | return result; |
| 38 | } |
| 39 | |
| 40 | int main(int argc, char* argv[]) { |
| 41 | if (argc < 3) { |