Find minimum continuous subsequence tags
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的更多相关文章
- [LeetCode] Minimum Window Subsequence 最小窗口序列
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of ...
- [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 ...
- [LintCode] Longest Increasing Continuous Subsequence 最长连续递增子序列
Give an integer array,find the longest increasing continuous subsequence in this array. An increasin ...
- Lintcode397 Longest Increasing Continuous Subsequence solution 题解
[题目描述] Give an integer array,find the longest increasing continuous subsequence in this array. An in ...
- [LintCode] Longest Increasing Continuous subsequence
http://www.lintcode.com/en/problem/longest-increasing-continuous-subsequence/# Give you an integer a ...
- LintCode 397: Longest Increasing Continuous Subsequence
LintCode 397: Longest Increasing Continuous Subsequence 题目描述 给定一个整数数组(下标从0到n - 1,n表示整个数组的规模),请找出该数组中 ...
- 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 ...
- [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof ...
- LintCode "Longest Increasing Continuous subsequence II" !!
DFS + Memorized Search (DP) class Solution { int dfs(int i, int j, int row, int col, vector<vecto ...
随机推荐
- 玩转X-CTR100 l STM32F4 l OLED显示-SSD1306无字库
我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] OLED显示屏具有自发光特性,不需要背光, ...
- 转:ios导航栏设置
原帖:http://www.cocoachina.com/industry/20131104/7287.html 本文提供的代码需要用Xcode 5来执行.如果你还在使用老版本的Xcode,那么在运行 ...
- Linux中MySQL中文乱码问题
一. 问题描述 登录后查看mysql默认编码: mysql> show variables like 'character%'; +--------------------------+---- ...
- SWIFT中的repeat...while
SWIFT中的repeat...while类似于JAVA\.NET中的 do while.大同小异只是把do换成了repeat var index = 10 repeat{ print(index) ...
- Windows-CreateProcess-lpsiStartInfo-STARTUPINFO-dwFlags
dwFlags: 简单地告诉CreateProcess函数结构中哪些成员有效: STARTF_USESIZE:使用dwXSize和dwYSize STARTF_USESHOWWINDOWS: wSho ...
- Luogu 3245 大数
Luogu 3245 大数 开始就想 \(10\) 进制 \(hash\) ,\(Hash(r)\equiv Hash(l-1)\cdot 10^{r-l+1}\) ,感觉没什么美妙的性质啊... 然 ...
- 51Nod:独木舟问题(贪心)
n个人,已知每个人体重,独木舟承重固定,每只独木舟最多坐两个人,可以坐一个人或者两个人.显然要求总重量不超过独木舟承重,假设每个人体重也不超过独木舟承重,问最少需要几只独木舟? 输入 第一行包含两个正 ...
- 放苹果问题 DP计数 m个苹果放在n个盘子里,苹果,盘子相同,盘子可为空
详细的解释放苹果问题的链接:苹果可相同可不同,盘子可相同可不同,盘子可空和不可空,都有详细的说明··· http://www.cnblogs.com/celia01/archive/2012/02/1 ...
- php7 安装swoole4.0.4
下载 https://codeload.github.com/swoole/swoole-src/tar.gz/swoole-4.0.4 tar zxvf swoole-4.0.4 mv swoole ...
- sql server 循环操作
使用的sql 语句如下: declare @userid int ;set @userid=0while(@userid<20)begin print 'the result is :'+STR ...