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. SharePoint Word Service-PowerShell

    1. 配置转换进程 Set-SPWordConversionServiceApplication –Identity "Word Automation Services" –Act ...

  2. L241

    Parents, try to get enough sleep to role model good habits to children. Bessesen notes that some med ...

  3. mysql数据库索引相关

    一 介绍 什么是索引? 索引在MySQL中也叫做“键”,是存储引擎用于快速找到记录的一种数据结构.索引对于良好的性能非常关键,尤其是当表中的数据量越来越大时,索引对于性能的影响愈发重要.索引优化应该是 ...

  4. ACCESS表的视图

    ACCESS表的视图 点击:   发布日期:2007-8-31 6:37:00   进入论坛     表是关系型数据库的基本结构.在Access中,表是一种关系特定主题的数据集合,如产品.供应商等.为 ...

  5. UI基础:UINavigationController、界面通信

    UINavigationControlle UINavigationController:导航控制器,是iOS中最常用的多视图控制器之一,它用来管理多个视图控制器.也称为多视图控制器. 导航控制器可以 ...

  6. 如何修改magento产品详细页面的栏目

    magento默认模板里面的产品信息页面的布局是以两栏带右侧栏显示的,那么如何修改为两栏带左侧栏或者三栏.一栏的方式显示呢?下面教大家一种很简单的方法就可以实现.下面是默认的布局预览:

  7. linux配置禁用启用IPv6

    IPv6被认为是IPv4的替代产品,它用来解决现有IPv4地址空间即将耗尽的问题.但目前,开启IPv6可能会导致一些问题.因此有时我们需要关闭IPv6.下面是IPv6的关闭方法应该适用于所有主流的Li ...

  8. 2018CCPC女生赛(树上莫队)

    签到题这里久懒得写了. B - 缺失的数据范围 Total Submission(s): 2602    Accepted Submission(s): 559 题意:求最大的N,满足N^a*[log ...

  9. 《DSP using MATLAB》Problem 3.5

    定义为: 如果序列绝对可和,其DTFT就存在.

  10. hdu2090-2097

    hdu2090 模拟 #include<stdio.h> int main(){ ]; ,a1,a2; ; while(scanf("%s%lf%lf",b,& ...