博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ST算法 Sliding Window algorithm template
阅读量:4699 次
发布时间:2019-06-09

本文共 4754 字,大约阅读时间需要 15 分钟。

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
slidingWindowTemplateByHarryChaoyangHe(String s, String t) { //init a collection or int value to save the result according the question. List
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
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
slidingWindowTemplate(string s,string p){ // define a vector store the result position vector
result; // string not have anagram if(s.size()
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
0) counter++; } left++; } } return result; }}

最容易理解的版本

// 解决实际问题class Solution {public:    vector
findAnagrams(string s, string p) { vector
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:

转载于:https://www.cnblogs.com/GeekDanny/p/10006138.html

你可能感兴趣的文章
AFNetwork 作用和用法详解
查看>>
20135219洪韶武——信息安全系统设计基础第二周学习总结
查看>>
jsf:selectmanycheckbox and selectbooleancheckbox问题
查看>>
【备忘录】使用eventHub非父子组件通信,报TypeError: cbs[i].apply is not a function
查看>>
[编译] g++ 与 Makefile
查看>>
STM32F10x_RTC日历
查看>>
JQuery插件的一般写法
查看>>
MVC公司架构介绍-序列化属性
查看>>
select count(1) from table where ..这句sql语句的作用
查看>>
六、传统的映射文件
查看>>
js实现事件模型bind与trigger
查看>>
import和export使用
查看>>
Android SDK Manager 一闪而过
查看>>
HiveSql调优经验
查看>>
mysql热备脚本
查看>>
c++虚函数,纯虚函数
查看>>
cdoj 秋实大哥与战争
查看>>
Js打开网页后居中显示
查看>>
系统测试中需要注意的点
查看>>
Rs232、Rs485、CAN总线比较区别总结
查看>>