Minimum Window Subsequence-LintCode

204 篇文章 0 订阅

Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of W.

If there is no such window in S that covers all characters in T, return the empty string “”. If there are multiple such minimum-length windows, return the one with the left-most starting index.

 注意事项
All the strings in the input will only contain lowercase letters.
The length of S will be in the range [1, 20000].
The length of T will be in the range [1, 100].

样例
Given S = “abcdebdde”, T = “bde”
Return “bcde”

Explanation: 
"bcde" is the answer because it occurs before "bdde" which has the same length.
"deb" is not a smaller window because the elements of T in the window must occur in order.

一刷,超时。

#ifndef C857_H
#define C857_H
#include<iostream>
#include<string>
#include<map>
using namespace std;
class Solution {
public:
    /**
    * @param S: a string
    * @param T: a string
    * @return: the minimum substring of S
    */
    string minWindow(string &S, string &T) {
        // Write your code here
        int len = S.size();
        int size = T.size();
        string res;
        int length = len;
        map<int, string> m;
        for (int i = 1; i <= len; ++i)
            m[i*len + (i - 1)] = T;
        for (int i = 1; i <= len; ++i)
        {
            for (int j = i; j <= len; ++j)
            {
                int tmp = i*len + (j - 1);
                if (!m[tmp].empty() && S[j - 1] == m[tmp][0])
                {
                    string data = m[tmp];
                    m[i*len + j] = data.erase(0, 1);
                }
                else
                    m[i*len + j] = m[tmp];
            }
        }
        for (int i = 1; i <= len; ++i)
        {
            for (int j = i; j <= len; ++j)
            {
                if (m[i*len+j] == ""&&length>j - i + 1)
                {
                    length = j - i + 1;
                    res = S.substr(i - 1, length);
                }
            }
        }
        return res;
    }
};
#endif

二刷,AC
重新构建dp,dp[i][j] = k表示T[0,j]时S[k,i]的子串且S[k,i]最短,k = -1表示T[0,j]不是S[0,i]的子串。当j = 0,若S[i] = T[0],dp[i][0] = i,表示T[0]时S[i,i]的子串。对于T[0,j]找到S[k,i]满足T[0,j]时S[k,i]的子串,遍历i,当k != -1,表示T[0,j-1]是S[k,i]的子串 ,i++,若T[j]=S[i],则T[0,j]是S[k,i]的子串,dp[i][j]=k。

#ifndef C857_H
#define C857_H
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Solution {
public:
    /**
    * @param S: a string
    * @param T: a string
    * @return: the minimum substring of S
    */
    string minWindow(string &S, string &T) {
        // Write your code here
        int len = S.size();
        int size = T.size();
        string res;
        int length = INT_MAX;
        //dp[i][j]=k表示T[0,j]时S[k,i]的子串且S[k,i]最短
        vector<vector<int>> dp(len, vector<int>(size));
        //当S[i]==T[0],T[0]是S[i]的子串,其它情况置为-1
        for (int i = 0; i < len; ++i)
        {
            dp[i][0] = -1;
            if (S[i] == T[0])
                dp[i][0] = i;
        }
        //当k!=-1,表示T[0,j-1]是S[k,i]的子串
        //i++,若T[j]=S[i],则T[0,j]是S[k,i]的子串,dp[i][j]=k
        for (int j = 1; j < size; ++j)
        {
            int k = -1;
            for (int i = 0; i < len; ++i)
            {
                dp[i][j] = -1;
                if (k != -1 && S[i] == T[j])
                    dp[i][j] = k;
                if (dp[i][j - 1] != -1)
                    k = dp[i][j - 1];
            }
        }
        //找出S满足条件的长度最短的子串,且保证长度相同的子串,优先输出下标小的
        int start = -1;
        for (int i = 0; i<len; ++i)
        {
            if (dp[i][size - 1] != -1)
            { 
                if (i - dp[i][size - 1] + 1 < length)
                {
                    length = i - dp[i][size - 1] + 1;
                    start = dp[i][size - 1];
                }
                else if (i - dp[i][size - 1] + 1 == length)
                    start = minVal(start, dp[i][size - 1]);
            }
        }
        return length==INT_MAX?"":S.substr(start,length);
    }
    int minVal(int a, int b)
    {
        return a < b ? a : b;
    }
};
#endif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值