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

algorithm template to slove substring search problems

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

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

  1. Generally speaking a sliding window is a sub-list that runs over an underlying collection. I.e., if you have an array like
  2. [a b c d e f g h]
  3. a sliding window of size 3 would run over it like
  4. [a b c]
  5. [b c d]
  6. [c d e]
  7. [d e f]
  8. [e f g]
  9. [f g h]

implement the template with java

  1. public class Solution {
  2. public List<Integer> slidingWindowTemplateByHarryChaoyangHe(String s, String t) {
  3. //init a collection or int value to save the result according the question.
  4. List<Integer> result = new LinkedList<>();
  5. if(t.length()> s.length()) return result;
  6. //create a hashmap to save the Characters of the target substring.
  7. //(K, V) = (Character, Frequence of the Characters)
  8. Map<Character, Integer> map = new HashMap<>();
  9. for(char c : t.toCharArray()){
  10. map.put(c, map.getOrDefault(c, 0) + 1);
  11. }
  12. //maintain a counter to check whether match the target string.
  13. int counter = map.size();//must be the map size, NOT the string size because the char may be duplicate.
  14. //Two Pointers: begin - left pointer of the window; end - right pointer of the window
  15. int begin = 0, end = 0;
  16. //the length of the substring which match the target string.
  17. int len = Integer.MAX_VALUE;
  18. //loop at the begining of the source string
  19. while(end < s.length()){
  20. char c = s.charAt(end);//get a character
  21. if( map.containsKey(c) ){
  22. map.put(c, map.get(c)-1);// plus or minus one
  23. if(map.get(c) == 0) counter--;//modify the counter according the requirement(different condition).
  24. }
  25. end++;
  26. //increase begin pointer to make it invalid/valid again
  27. while(counter == 0 /* counter condition. different question may have different condition */){
  28. char tempc = s.charAt(begin);//***be careful here: choose the char at begin pointer, NOT the end pointer
  29. if(map.containsKey(tempc)){
  30. map.put(tempc, map.get(tempc) + 1);//plus or minus one
  31. if(map.get(tempc) > 0) counter++;//modify the counter according the requirement(different condition).
  32. }
  33. /* save / update(min/max) the result if find a target*/
  34. // result collections or result int value
  35. begin++;
  36. }
  37. }
  38. return result;
  39. }
  40. }

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

  1. class Solution{
  2. public:
  3. vector<int> slidingWindowTemplate(string s,string p){
  4. // define a vector store the result position
  5. vector<int> result;
  6. // string not have anagram
  7. if(s.size()<p.size())
  8. return result;
  9. map<char,int> hash_map;
  10. // hash map string p
  11. for(auto &c:p)
  12. map[p]++;
  13. // use counter store the hash map size
  14. int counter=map.size();
  15. // define two pointer,point to the window boundary
  16. int left=0,right=0;
  17. // the substring length
  18. int len=INT_MAX;
  19. for(right<s.size()){
  20. char c=s[right];
  21. if (hash_map.contains(c)){
  22. hash_map[c]--;
  23. if(hash_map[c]==0)
  24. counter--;
  25. }
  26. right++;
  27. while(counter==0){
  28. char temp_c=s[left];
  29. if(hash_map.cotains(c)){
  30. map[temp_c]++;
  31. if(hash_map[temp_c]>0)
  32. counter++;
  33. }
  34. left++;
  35. }
  36. }
  37. return result;
  38. }
  39. }

最容易理解的版本

  1. // 解决实际问题
  2. class Solution {
  3. public:
  4. vector<int> findAnagrams(string s, string p) {
  5. vector<int> pv(26,0), sv(26,0), res;
  6. if(s.size() < p.size())
  7. return res;
  8. // fill pv, vector of counters for pattern string and sv, vector of counters for the sliding window
  9. for(int i = 0; i < p.size(); ++i)
  10. {
  11. ++pv[p[i]-'a'];
  12. ++sv[s[i]-'a'];
  13. }
  14. if(pv == sv)
  15. res.push_back(0);
  16. //here window is moving from left to right across the string.
  17. //window size is p.size(), so s.size()-p.size() moves are made
  18. for(int i = p.size(); i < s.size(); ++i)
  19. {
  20. // window extends one step to the right. counter for s[i] is incremented
  21. ++sv[s[i]-'a'];
  22. // since we added one element to the right,
  23. // one element to the left should be forgotten.
  24. //counter for s[i-p.size()] is decremented
  25. --sv[s[i-p.size()]-'a'];
  26. // if after move to the right the anagram can be composed,
  27. // add new position of window's left point to the result
  28. if(pv == sv)
  29. res.push_back(i-p.size()+1);
  30. }
  31. return res;
  32. }
  33. };

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. Spring MVC中的 权限拦截定义 以及 权限拦截的研发步骤

    权限拦截 (拦截器: 对请求进行区分) 1 实现的价值(作用) 用户未登录:访问没用登录的URL,拦截到以后 跳转回登录 用户未登录:访问登录的URL,直接放行到后续流程处理框架,进行后续的操作 用户 ...

  2. JavaOOP 对象和封装

    1.后缀:jsp---相当于html,但是它里面可以写java代码. 2.包名取名规则 a.网站域名倒着写 b.字母小写 3.类名取名规则 a.首字母大写 4.三目运算(适用简单的if-else) 条 ...

  3. HTTP_5_通信数据转发程序:代理、网关、隧道

    HTTP通信时,除客户端和服务器之外,还有一些用于通信数据转发的应用程序,例如代理,网关,隧道.配合服务器工作. 代理 转发功能,客户端与服务器之间可有多个代理, 缓存代理:减少服务器压力,透明代理: ...

  4. javaweb入门-----request与response的作用

    request对象和request对象的原理 1.request和response对象request对象和request对象的原理时由服务器创建的,我们来使用它们 2.request对象是来获取请求消 ...

  5. Hadoop MapReduce的Shuffle过程

    一.概述 理解Hadoop的Shuffle过程是一个大数据工程师必须的,笔者自己将学习笔记记录下来,以便以后方便复习查看. 二. MapReduce确保每个reducer的输入都是按键排序的.系统执行 ...

  6. C#中属性的解析

    一.域的概念 C#中域是指成员变量和方法,在OOP编程中(面向对象编程)我们要求用户只知道类是干什么的,而不许知道如何完成的,或者说不允许访问类的内部,对于有必要在类外可见的域,我们用属性来表达,所以 ...

  7. 如何使用Arrays工具类操作数组

    介绍 我们要先知道Arrays 是什么. java.util.Arrays 类是 JDK 提供的一个工具类主要用来操作数组,比如数组的复制转换等各种方法,Arrays 的方法都是静态方法可以通过Arr ...

  8. c#将字符串转化为合理的文件名

    string name = System.Text.RegularExpressions.Regex.Replace(url, "[<>/\\|:\"?*]" ...

  9. Activity 使用详解

    极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以 ...

  10. FTP服务端部署

    FTP服务端搭建(本地用户登入:使用本地用户和密码登入)1.文件配置:vsftpd.conf: 主配置文件ftpusers: 指定哪些用户不能访问FTP服务器user_list: 指定的用户是否可以访 ...