zoukankan      html  css  js  c++  java
  • LeetCode Minimum Window Substring

    class Solution {
    private:
        int shd_cnt[256];
        int cur_cnt[256];
    public:
        string minWindow(string S, string T) {
            int slen = S.length();
            int tlen = T.length();
            
            // array to store char stat info of string T
            for (int i=0; i<256; i++) shd_cnt[i] = cur_cnt[i] = 0;
            // collect char stat info of string T
            for (int i=0; i<tlen; i++) shd_cnt[T[i]]++;
            
            int shd_match_cnt = 0, cur_match_cnt = 0;
            for (int i=0; i<256; i++) shd_match_cnt += shd_cnt[i] != 0;
            
            int start = 0, end = 0;
            
            int mlen = INT_MAX, ps = 0, pe = 0;
            
            bool updated = true;
            
            while (updated) {
                updated = false;
                while (cur_match_cnt == shd_match_cnt && start < slen) {
                    if (end - start < mlen) {
                        mlen = end - start;
                        ps = start, pe = end;
                    }
                    
                    char ch = S[start];
                    // whether current char is critical for matching
                    // if so it could not be omitted
                    if (cur_cnt[ch] - 1 < shd_cnt[ch]) break;
    
                    updated = true;
                    cur_cnt[ch]--;
                    start++;
                }
                
                if (end < slen) {
                    updated = true;
                    char ch = S[end++];
                    
                    if (++cur_cnt[ch] == shd_cnt[ch]) { // using strict equal
                        cur_match_cnt++;
                    }
                }
            }
            return S.substr(ps, pe - ps);
        }
    };

    "滑动窗口"...

    重新写感觉不行啊

    class Solution {
    public:
        string minWindow(string s, string t) {
            int cnt[128] = {0};
            int distinct = 0;
            int slen = s.size();
            int tlen = t.size();
            
            for (int i=0; i<tlen; i++) {
                if (!cnt[t[i]]++) {
                    distinct++;
                }
            }
            int matched = 0;
            int mcnt[128] = {0};
            int p=0;
            int q=0;
            
            int minlen = INT_MAX;
            int minstart = 0;
            
            while (q < slen) {
                char ch = s[q];
                if (cnt[ch]) {
                    if (++mcnt[ch] == cnt[ch]) {
                        matched++;
                    }
                }
                q++;
                if (matched != distinct) {
                    continue;
                }
                while (p < q) {
                    if (!cnt[s[p]]) {
                        p++;
                        continue;
                    }
                    if (mcnt[s[p]] > cnt[s[p]]) {
                        mcnt[s[p]]--;
                        p++;
                    } else {
                        break;
                    }
                }
                
                if (q - p < minlen) {
                    minstart = p;
                    minlen = q - p;
                }
            }
            minlen = distinct == matched ? minlen : 0;
            return s.substr(minstart, minlen);
        }
    };
  • 相关阅读:
    python利用Trie(前缀树)实现搜索引擎中关键字输入提示(学习Hash Trie和Double-array Trie)
    利用trie树实现前缀输入提示及trie的python实现
    利用python实现简单词频统计、构建词云
    Xss-challenge-tour(1-10)
    CTFHub-SQL注入 思路
    Bugku-web40
    Buuctf-web-[极客大挑战 2019]HardSQL
    命令执行及代码执行漏洞
    CTFHub-RCE 思路&AWCTF部分web题
    upload-labs 11-12 00截断
  • 原文地址:https://www.cnblogs.com/lailailai/p/3975780.html
Copyright © 2011-2022 走看看