Given targetList, a list of strings representing the desired tags, and availableTagList, a list of strings representing the sequence of all available tags. Find the start index and end index of the minimus continuous subsequence containing all the desired tags in any order. If more that one results, return the one with the smaller start index.

Note:

  • No dup tags in targetList.
  • Returned subsequence may include additional tags beyond those in targetList.
  • Consider only alphanumeric characters.
  • Output 0 if no match.

  • 字符串中等题。Sliding window algorithem + Hash。
  • 思想跟其他滑动窗口题一样,不同的是此题没有给出window size,而是要找最小window。还是对targetList建立hash表,然后扩展window end point去找到能包含所有targetList tags的window。找到之后,再移动window start point。这里跟其他题目不一样的地方是,window size并不固定,而且需要找最小window size,所以在满足count == 0条件下,扩展start point来缩小window,直到当前window已经不能包含所有tags。
 //
// main.cpp
// LeetCode
//
// Created by Hao on 2017/3/16.
// Copyright © 2017年 Hao. All rights reserved.
// #include <iostream>
#include <vector>
#include <unordered_map>
using namespace std; class Solution {
public:
vector<int> subSequenceTags(vector<string> targetList, vector<string> availableTagList) {
vector<int> vResult; // corner case
if (targetList.empty() || availableTagList.empty()) {
vResult.push_back();
return vResult;
} unordered_map<string, int> hash; for (auto & it : targetList)
++ hash[it]; // window start/end point, hit count ( sum { positive hash[i] } ), min result length, result start index
int left = , right = , count = targetList.size(), len = INT_MAX, start = ; while (right < availableTagList.size()) {
if (hash[availableTagList.at(right)] > ) // could use == 1 as well coz that no dup in targetList
-- count; -- hash[availableTagList.at(right)]; ++ right; // continue move window start point rightward to minimize window until condition not valid
while ( == count) {
if (right - left < len) {
len = right - left;
start = left;
} if (hash[availableTagList.at(left)] >= ) // could change the condition to == 0 if condition for -- count varies
++ count; ++ hash[availableTagList.at(left)]; ++ left;
}
} if (len != INT_MAX) {
vResult.push_back(start);
vResult.push_back(start + len - );
} else
vResult.push_back(); // Don't miss the empty result if no match return vResult;
}
}; int main(int argc, char* argv[])
{
Solution testSolution; vector<vector<string>> tInputs = {{"made", "in", "spain"}, {"2abc","bcd", "cab"}, {"in", "the", "spain"}, {"in"}, {}, {"in"}};
vector<vector<string>> aInputs = {{"made", "weather", "forecast", "says", "that", "made", "rain", "in", "spain", "stays"},
{"dbc", "2abc", "cab", "bcd", "bcb"},
{"the", "spain", "that", "the", "rain", "in", "spain", "stays", "forecast", "in", "the"},
{"aa", "bb", "cc"},
{"aa", "bb", "cc"},
{}};
vector<int> result; /*
5 8
1 3
3 6
0
0
0
*/
for (auto i = ; i < tInputs.size(); ++ i) {
result = testSolution.subSequenceTags(tInputs[i], aInputs[i]); for (auto it : result)
cout << it << " ";
cout << endl;
} return ;
}

Find minimum continuous subsequence tags的更多相关文章

  1. [LeetCode] Minimum Window Subsequence 最小窗口序列

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

  2. [LeetCode] 727. Minimum Window Subsequence 最小窗口序列

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

  3. [LintCode] Longest Increasing Continuous Subsequence 最长连续递增子序列

    Give an integer array,find the longest increasing continuous subsequence in this array. An increasin ...

  4. Lintcode397 Longest Increasing Continuous Subsequence solution 题解

    [题目描述] Give an integer array,find the longest increasing continuous subsequence in this array. An in ...

  5. [LintCode] Longest Increasing Continuous subsequence

    http://www.lintcode.com/en/problem/longest-increasing-continuous-subsequence/# Give you an integer a ...

  6. LintCode 397: Longest Increasing Continuous Subsequence

    LintCode 397: Longest Increasing Continuous Subsequence 题目描述 给定一个整数数组(下标从0到n - 1,n表示整个数组的规模),请找出该数组中 ...

  7. LC 727. Minimum Window Subsequence 【lock,hard】

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

  8. [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列

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

  9. LintCode "Longest Increasing Continuous subsequence II" !!

    DFS + Memorized Search (DP) class Solution { int dfs(int i, int j, int row, int col, vector<vecto ...

随机推荐

  1. ArcEngine 9.3与64位操作系统 冲突

    ArcEngine 9.3与64位操作系统 冲突 2011年03月30日 星期三 11:13 错误信息: 未处理 System.TypeInitializationException  Message ...

  2. IDC:2014年的十大 IT 趋势

    IDC:2014年的十大 IT 趋势 市场研究公司 IDC 近日发布报告,对 2014 年的十大科技行业发展趋势作出了预测.IDC 称,2014 年将是科技业"鏖战正酣"的一年,整 ...

  3. [转]CentOS 6和CentOS 7防火墙的关闭

      CentOS6.5查看防火墙的状态: 1 [linuxidc@localhost ~]$service iptable status 显示结果: 1 2 3 4 5 [linuxidc@local ...

  4. nw 系统托盘的添加方式,以及ajax失效问题

    1.nw 系统托盘的添加方式 /** ------------------------------------------------------------ 最小化托盘 -------------- ...

  5. Linux:rm:du命令

    RM 删除选项rm -r 递归删除,删除目录下所有 删除当前文件下所有文件呢? rm -rf * rm -f 忽略删除提醒 万千从中找到文件删除 ls |grep abc |xargs rm -f 保 ...

  6. Tensorflow中的滑动平均模型

    原文链接 在Tensorflow的教程里面,使用梯度下降算法训练神经网络时,都会提到一个使模型更加健壮的策略,即滑动平均模型. 基本思想 在使用梯度下降算法训练模型时,每次更新权重时,为每个权重维护一 ...

  7. windows下配置redis

    1.首先去GitHub上下载所需文件,这里我们下载的是zip文件 https://github.com/MicrosoftArchive/redis/releases 2.解压后文件目录如下 3.启动 ...

  8. Vue2.x directive自定义指令

    directive自定义指令 除了默认设置的核心指令( v-model 和 v-show ),Vue 也允许注册自定义指令. 注意,在 Vue2.0 里面,代码复用的主要形式和抽象是组件——然而,有的 ...

  9. MySQL--查询表统计信息

    ============================================================= 可以用show table status 来查看表的信息,如:show ta ...

  10. leetcode:Longest Common Prefix【Python版】

    1.当strs为空,直接输出“” 2.当strs中含有“”,直接输出“” 3.strs[0]的最长长度由最短公共长度l决定(code line:15) class Solution: # @retur ...