zoukankan      html  css  js  c++  java
  • LeetCode "Repeated DNA Sequence"

    Typical rolling-hash solution. That is, Boyer-Moore algorithm variation.

    class Solution {
    public:
        inline int encode(char c)
        {
            switch (c)
            {
            case 'A': return 0;
            case 'C': return 1;
            case 'G': return 2;
            case 'T': return 3;
            }
            return 0;
        }
        vector<string> findRepeatedDnaSequences(string s)
        {
            vector<string> ret;
    
            size_t len = s.length();
            if (len < 11) return ret;
    
            unordered_set<unsigned long> rec;
            unordered_set<string> rec_s;
    
            //    init
            unsigned long hash = 0;
            for (int i = 0; i < 10; i++)
            {
                hash *= 4;
                hash += encode(s[i]);
            }
            rec.insert(hash);
    
            //    go
            for (int i = 10; i < len; i++)
            {            
                hash *= 4;
                hash += encode(s[i]);
                hash &= (1 << 20) - 1;
    
                if (rec.find(hash) != rec.end())
                {
                    string ts = s.substr(i - 9, 10);
                    if (rec_s.find(ts) == rec_s.end())
                    {
                        rec_s.insert(ts);
                        ret.push_back(ts);
                    }
                }
                else
                {
                    rec.insert(hash);
                }
            }
    
            return ret;
        }
    };
  • 相关阅读:
    zbb20180930 Postman 使用方法详解
    Cookie、Session、jsp、EL、JSTL
    Http协议、Tomcat、servlet
    xml、网络编程、 反射
    JDBC、DBUtils
    Java IO流对象、多线程
    mySql
    Java基础
    VueJs
    GIT
  • 原文地址:https://www.cnblogs.com/tonix/p/4280798.html
Copyright © 2011-2022 走看看