zoukankan      html  css  js  c++  java
  • 1244. Minimum Genetic Mutation

    描述

    A gene string can be represented by an 8-character long string, with choices from "A", "C", "G", "T".

    Suppose we need to investigate about a mutation (mutation from "start" to "end"), where ONE mutation is defined as ONE single character changed in the gene string.

    For example, "AACCGGTT" -> "AACCGGTA" is 1 mutation.

    Also, there is a given gene "bank", which records all the valid gene mutations. A gene must be in the bank to make it a valid gene string.

    Now, given 3 things - start, end, bank, your task is to determine what is the minimum number of mutations needed to mutate from "start" to "end". If there is no such a mutation, return -1.

    1.Starting point is assumed to be valid, so it might not be included in the bank.
    2.If multiple mutations are needed, all mutations during in the sequence must be valid.
    3.You may assume start and end string is not the same.

    样例

    Example 1:

    start: "AACCGGTT"
    end: "AACCGGTA"
    bank: ["AACCGGTA"]

    return: 1

    Example 2:

    start: "AACCGGTT"
    end: "AAACGGTA"
    bank: ["AACCGGTA", "AACCGCTA", "AAACGGTA"]

    return: 2

    Example 3:

    start: "AAAAACCC"
    end: "AACCCCCC"
    bank: ["AAAACCCC", "AAACCCCC", "AACCCCCC"]

    return: 3

    class Solution {
    public:
        /**
         * @param start: 
         * @param end: 
         * @param bank: 
         * @return: the minimum number of mutations needed to mutate from "start" to "end"
         */
        int minMutation(string &start, string &end, vector<string> &bank) {
            // Write your code here
            if (bank.empty()) return -1;
            vector<char> gens{'A','C','G','T'};
            unordered_set<string> s{bank.begin(), bank.end()};
            unordered_set<string> visited;
            queue<string> q{{start}};
            int level = 0;
            while (!q.empty()) {
                int len = q.size();
                for (int i = 0; i < len; ++i) {
                    string t = q.front(); q.pop();
                    if (t == end) return level;
                    for (int j = 0; j < t.size(); ++j) {
                        char old = t[j];
                        for (char c : gens) {
                            t[j] = c;
                            if (s.count(t) && !visited.count(t)) {
                                visited.insert(t);
                                q.push(t);
                            }
                        }
                        t[j] = old;
                    }
                }
                ++level;
            }
            return -1;
        }
    };
    
  • 相关阅读:
    B1009
    (OK)(OK) [android-x86-6.0-rc1] compile_Android-x86_64_in_IBM-X3650-M4.txt
    Fortran, Matlab, Octave, Scilab计算速度比较
    GNU Octave
    [android-x86-6.0-rc1] /system/etc/init.sh
    [android-x86-6.0-rc1] /system/xbin/log.sh
    Android源码学习之接着浅析SystemServer
    Android源码学习之浅析SystemServer脉络
    Android-x86_64
    Android-x86_64
  • 原文地址:https://www.cnblogs.com/narjaja/p/9810581.html
Copyright © 2011-2022 走看看