ST算法(Sliding Window):A easy way to slove the substring problems

algorithm template to slove substring search problems

这是一个利用哈希思想解决字符串问题,特别是关于子字符串的问题的模版

方法描述: 怎样实现滑动窗口

Generally speaking a sliding window is a sub-list that runs over an underlying collection. I.e., if you have an array like

[a b c d e f g h]

a sliding window of size 3 would run over it like

[a b c]
[b c d]
[c d e]
[d e f]
[e f g]
[f g h]

implement the template with java

public class Solution {
public List<Integer> slidingWindowTemplateByHarryChaoyangHe(String s, String t) {
//init a collection or int value to save the result according the question.
List<Integer> result = new LinkedList<>();
if(t.length()> s.length()) return result; //create a hashmap to save the Characters of the target substring.
//(K, V) = (Character, Frequence of the Characters)
Map<Character, Integer> map = new HashMap<>();
for(char c : t.toCharArray()){
map.put(c, map.getOrDefault(c, 0) + 1);
}
//maintain a counter to check whether match the target string.
int counter = map.size();//must be the map size, NOT the string size because the char may be duplicate. //Two Pointers: begin - left pointer of the window; end - right pointer of the window
int begin = 0, end = 0; //the length of the substring which match the target string.
int len = Integer.MAX_VALUE; //loop at the begining of the source string
while(end < s.length()){ char c = s.charAt(end);//get a character if( map.containsKey(c) ){
map.put(c, map.get(c)-1);// plus or minus one
if(map.get(c) == 0) counter--;//modify the counter according the requirement(different condition).
}
end++; //increase begin pointer to make it invalid/valid again
while(counter == 0 /* counter condition. different question may have different condition */){ char tempc = s.charAt(begin);//***be careful here: choose the char at begin pointer, NOT the end pointer
if(map.containsKey(tempc)){
map.put(tempc, map.get(tempc) + 1);//plus or minus one
if(map.get(tempc) > 0) counter++;//modify the counter according the requirement(different condition).
} /* save / update(min/max) the result if find a target*/
// result collections or result int value begin++;
}
}
return result;
}
}

实现滑动窗口算法的c++模版

class Solution{
public:
vector<int> slidingWindowTemplate(string s,string p){
// define a vector store the result position
vector<int> result;
// string not have anagram
if(s.size()<p.size())
return result;
map<char,int> hash_map;
// hash map string p
for(auto &c:p)
map[p]++;
// use counter store the hash map size
int counter=map.size();
// define two pointer,point to the window boundary
int left=0,right=0;
// the substring length
int len=INT_MAX;
for(right<s.size()){
char c=s[right];
if (hash_map.contains(c)){
hash_map[c]--;
if(hash_map[c]==0)
counter--;
}
right++;
while(counter==0){
char temp_c=s[left];
if(hash_map.cotains(c)){
map[temp_c]++;
if(hash_map[temp_c]>0)
counter++;
}
left++;
}
}
return result; }
}

最容易理解的版本

// 解决实际问题
class Solution {
public:
vector<int> findAnagrams(string s, string p) {
vector<int> pv(26,0), sv(26,0), res;
if(s.size() < p.size())
return res;
// fill pv, vector of counters for pattern string and sv, vector of counters for the sliding window
for(int i = 0; i < p.size(); ++i)
{
++pv[p[i]-'a'];
++sv[s[i]-'a'];
}
if(pv == sv)
res.push_back(0); //here window is moving from left to right across the string.
//window size is p.size(), so s.size()-p.size() moves are made
for(int i = p.size(); i < s.size(); ++i)
{
// window extends one step to the right. counter for s[i] is incremented
++sv[s[i]-'a']; // since we added one element to the right,
// one element to the left should be forgotten.
//counter for s[i-p.size()] is decremented
--sv[s[i-p.size()]-'a']; // if after move to the right the anagram can be composed,
// add new position of window's left point to the result
if(pv == sv)
res.push_back(i-p.size()+1);
}
return res;
}
};

reference:

stackoverflow:what is sliding window algorithm

github:chaoyanghe silding window algortihm template

ST算法 Sliding Window algorithm template的更多相关文章

  1. 滑动窗口(Sliding Window)技巧总结

    什么是滑动窗口(Sliding Window) The Sliding Problem contains a sliding window which is a sub – list that run ...

  2. [总结]RMQ问题&ST算法

    目录 一.ST算法 二.ST算法の具体实现 1. 初始化 2. 求出ST表 3. 询问 三.例题 例1:P3865 [模板]ST表 例2:P2880 [USACO07JAN]平衡的阵容Balanced ...

  3. POJ 2823 Sliding Window ST RMQ

    Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is m ...

  4. 算法与数据结构基础 - 滑动窗口(Sliding Window)

    滑动窗口基础 滑动窗口常用来解决求字符串子串问题,借助map和计数器,其能在O(n)时间复杂度求子串问题.滑动窗口和双指针(Two pointers)有些类似,可以理解为往同一个方向走的双指针.常用滑 ...

  5. POJ 2823 Sliding Window + 单调队列

    一.概念介绍 1. 双端队列 双端队列是一种线性表,是一种特殊的队列,遵守先进先出的原则.双端队列支持以下4种操作: (1)   从队首删除 (2)   从队尾删除 (3)   从队尾插入 (4)   ...

  6. poj 2823 Sliding Window (单调队列入门)

    /***************************************************************** 题目: Sliding Window(poj 2823) 链接: ...

  7. CodeForces 359D (数论+二分+ST算法)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=47319 题目大意:给定一个序列,要求确定一个子序列,①使得该子序 ...

  8. POJ - 2823 Sliding Window (滑动窗口入门)

    An array of size n ≤ 10 6 is given to you. There is a sliding window of size kwhich is moving from t ...

  9. POJ 2823 Sliding Window 线段树

    http://poj.org/problem?id=2823 出太阳啦~^ ^被子拿去晒了~晚上还要数学建模,刚才躺在床上休息一下就睡着了,哼,还好我强大,没有感冒. 话说今年校运会怎么没下雨!!!说 ...

随机推荐

  1. 【iOS】Xcode 插件安装后无效

    今天把 Xcode 升级到 7.3.1,发现插件安装后无效…… 原因:苹果要求加入UUID证书从而保证插件的稳定性. 解决方法: 1. 查看Xcode的UUID 在终端输入以下命令: defaults ...

  2. poj 1068 模拟

    题目链接 大概题意就是告诉你有个n个小括号,每一个")"左边有多少个"("都告诉你了,然后让你求出每一对括号之间有多少对括号(包含自己本身). 思路: 我先计算 ...

  3. 破解EFCore扩展Dll --- Z.EntityFramework.Extensions.EFCore

    安装 Z.EntityFramework.Extensions.EFCore Install-Package Z.EntityFramework.Extensions.EFCore -Version ...

  4. 拉格朗日对偶性(Lagrange duality)

    目录 拉格朗日对偶性(Lagrange duality) 1. 从原始问题到对偶问题 2. 弱对偶与强对偶 3. KKT条件 Reference: 拉格朗日对偶性(Lagrange duality) ...

  5. c#异常后重试操作

    private void TryConnect(System.Action action)         {             int retries = 3;             whi ...

  6. Linux部署项目遇到问题解决

    使用Linux部署web项目,可能会遇到各种各样问题导致服务启动失败,以下是我近期部署项目遇到的问题以及解决方案 一.场景:把war包放入tomcat的webapps文件夹下,然后启动tomcat服务 ...

  7. Cannot attach the file “MvcMovie.mdf” as database “aspnet-MvcMovie”

    今天在微软开发人员官网上学习asp.net mvc5入门的时候,遇到一个棘手的问题,我是按照教程一步一步操作的,但期间遇到一个自己觉得莫名其妙的问题,教程中也没有提到这个, 在添加新字段这一章节,跟着 ...

  8. log4net服务启动后没有记录日志

    有时候在用log4net的时候,调试或执行是ok的,但是安装服务后没有记录日志. 这是因为服务启动是在C盘启动,而程序放的位置在别的目录. 这时候需要指定读取配置文件的位置为程序所在的目录 strin ...

  9. html的一些基本属性介绍

    一.html的属性类型: 1.常见标签属性: a.<h1>:align对其方式      例如:<h1  align="right"> hhhhh</ ...

  10. JavaWeb——JSP开发1

    1.什么是jsp,为什么要使用jsp. 再使用idea创建完一个web工程后,在webapp目录下会生成一个index.jsp 直接编译运行,网站将自动打开这样一个网页: 所以我们可以推测这个inde ...