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 ...
随机推荐
- (转)Secondary NameNode的作用
在Hadoop中,有一些命名不好的模块,Secondary NameNode是其中之一.从它的名字上看,它给人的感觉就像是NameNode的备份.但它实际上却不是.很多Hadoop的初学者都很疑惑,S ...
- C++静态成员变量和静态成员函数
数据成员可以分静态变量.非静态变量两种. 静态成员:静态类中的成员加入static修饰符,即是静态成员.可以直接使用类名+静态成员名访问此静态成员,因为静态成员存在于内存,非静态成员需要实例化才会分配 ...
- 玩转X-CTR100 l 平台-4WD智能小车
我造轮子,你造车,创客一起造起来!更多塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ] 本文介绍使用X-CTR100控制器搭建4WD智能 ...
- DevExpress v17.2新版亮点—WPF篇(五)
用户界面套包DevExpress v17.2终于正式发布,本站将以连载的形式为大家介绍各版本新增内容.本文将介绍了DevExpress WPF v17.2 新的Hamburger Menu.Sched ...
- DevExpress WPF入门指南:加载动画的应用
LoadingDecorator是一个容器控件用于显示 long-loading 的内容.内容还没加载完成的时候会显示一个加载指示器,加载完成后指示器消失,如下图所示: 开启LoadingDecora ...
- LibSVM文件转换为csv格式
Spark MLlib 的官方例子里面提供的数据大部分是 libsvm 格式的.这其实是一种非常蛋疼的文件格式,和常见的二维表格形式相去甚远,下图是里面的一个例子: libsvm 文件的基本格式如下: ...
- [转载]三款SDR平台对比:HackRF,bladeRF和USRP
这篇文章是 Taylor Killian 13年8月发表在自己的博客上的.他对比了三款平价的SDR平台,认为这三款产品将是未来一年中最受欢迎的SDR平台.我觉得这篇文章很有参考价值,简单翻译一份转过来 ...
- Dll封装dll,并且调用该封装的dll
按照图1,2,3创建一个默认的(可以导出符号的dll项目) 默认创建的,很好地给我们说明了怎么导出 变量,导出函数,导出类 注意这里加入你要导出的函数的声明 WIN32PROJECT1_API int ...
- react 部分ES6写法
react+react-router+antd 栗子:https://github.com/Aquarius1993/reactApp 模块: 1. 引入模块 import React from 'r ...
- No.01——配置编程环境
======由于很喜欢Android编程,所以买来了安卓权威指南来学习.为了应用费曼技巧——把知识输出出去以检验和巩固,在此写下学习笔记======= 1. 配置编程环境 Java的JDK(Java ...