Minimum_Window_Substring两种方法求解
题目描述:
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
For example,
S ="ADOBECODEBANC"
T ="ABC"
Minimum window is"BANC".
Note:
If there is no such window in S that covers all characters in T, return the emtpy string"".
If there are multiple such windows, you are guaranteed that there will always be only one unique minimum window in S.
分析
采用双指针遍历,尾指针遍历至包含T的所有字符,首指针尽可能向后压缩,然后在所有窗口中记录最小窗口位置。
PS: 以上两种方法均能通过本地测试,应该没有什么问题,第一种是根据网上别人写的代码改编的。
//给出一个长串(如:S = "ADOBECODEBANC")和一个短串(如:T = "ABC
//找出长串中包含短串的最小字符串(BANC)
//...
//方法一
#include <iostream>
#include <string>
#include <memory.h>
using namespace std; int main(){ string long_str, short_str;
int ShortMark[256], Ben2EndMark[256];
memset(ShortMark, 0, sizeof(ShortMark));
memset(Ben2EndMark, 0, sizeof(Ben2EndMark));
int ansbegin = 0, ansend = 0x3fffffff; freopen("F:\\input.txt", "r", stdin);
cin>>long_str>>short_str;
int count = 0;
for(int i = 0; i < short_str.length(); ++i) ShortMark[short_str[i]] ++; int p_begin = 0, p_end = 0;
while(p_end < long_str.length()){ //如果字符串(begin-end)中存在short_str中的字符,则Ben2EndMark[]对应位置+1;
if(ShortMark[long_str[p_end]]){
Ben2EndMark[long_str[p_end]]++; //如果统计的字符小于等于ShortMark中该字符的个数
if(Ben2EndMark[long_str[p_end]] <= ShortMark[long_str[p_end]])
count ++;
//如果(begin-end)中包含了short_str的所有字符
if(count == short_str.length()){ //cout<< ShortMark[long_str[p_begin]]<<endl;
//cout<< Ben2EndMark[long_str[p_begin]];
while(!ShortMark[long_str[p_begin]] || Ben2EndMark[long_str[p_begin]] > ShortMark[long_str[p_begin]]){
if(Ben2EndMark[long_str[p_begin]] > ShortMark[long_str[p_begin]])
-- Ben2EndMark[long_str[p_begin]];
++ p_begin;
}
//如果窗口更小则...
if(p_end - p_begin < ansend - ansbegin){
ansbegin = p_begin;
ansend = p_end;
} -- Ben2EndMark[long_str[p_begin]];
-- count;
++ p_begin;
}
}
++ p_end;
}
cout<< (ansend - ansbegin + 1);
} //方法二
//#include <iostream>
//#include <string>
//using namespace std;
//
//string long_str, short_str;
//
//bool is_exit(int pstart, int pend, string longstr){
// int count = 0;
// for(int j = 0; j < short_str.length(); ++j){
// for(int i = pstart; i <= pend; ++i){
// if(pstart <= longstr.find(short_str[j]) && longstr.find(short_str[j]) <= pend){
// count ++;
// break;
// }
// }
// }
// if(count == short_str.length())
// return true;
// else
// return false;
//}
//
//int main(){
//
// freopen("F:\\input.txt", "r", stdin);
// cin>>long_str>>short_str;
// int p_start = 0;
// int p_end;
// int LENGTH = 10000;
//
// for(int i = 0; i < long_str.length(); ++i){
// if(is_exit(0, i, long_str)){
// p_end = i;
// break;
// }
// }
// while(p_end <= long_str.length()){
// int TemLong = 10000;
// for(int i = 0; i <= p_end; ++i){
// string Substr = long_str.substr(0, p_end+1);
// string rSunstr(Substr.rbegin(), Substr.rend());//实现逆序排序
// if(is_exit(0, i, rSunstr))
// TemLong = i + 1;
// LENGTH = (LENGTH <= TemLong) ? LENGTH:TemLong;
// }
// ++p_end;
// }
// cout<< LENGTH;
// return 0;
//}
Minimum_Window_Substring两种方法求解的更多相关文章
- pat1067 在离散数学中置换群思想上可用并查集和递归两种方法求解问题
1.递归求解 注:叙述时 节点其实就是数字0-N-1 !!!最好用一个数组记录0-N-1每个数字的位置 !!!递归计算一个置换群内部的节点数 分为两种情况 累加M,M即是一个置换群所有数字在正确位置 ...
- HDU 1160 排序或者通过最短路两种方法解决
题目大意: 给定一堆点,具有x,y两个值 找到一组最多的序列,保证点由前到后,x严格上升,y严格下降,并把最大的数目和这一组根据点的编号输出来 这里用两种方法来求解: 1. 我们可以一开始就将数组根据 ...
- windows下获取IP地址的两种方法
windows下获取IP地址的两种方法: 一种可以获取IPv4和IPv6,但是需要WSAStartup: 一种只能取到IPv4,但是不需要WSAStartup: 如下: 方法一:(可以获取IPv4和I ...
- android 之 启动画面的两种方法
现在,当我们打开任意的一个app时,其中的大部分都会显示一个启动界面,展示本公司的logo和当前的版本,有的则直接把广告放到了上面.启动画面的可以分为两种设置方式:一种是两个Activity实现,和一 ...
- [转载]C#读写txt文件的两种方法介绍
C#读写txt文件的两种方法介绍 by 大龙哥 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char ...
- php如何防止图片盗用/盗链的两种方法(转)
图片防盗链有什么用? 防止其它网站盗用你的图片,浪费你宝贵的流量.本文章向大家介绍php防止图片盗用/盗链的两种方法 Apache图片重定向方法 设置images目录不充许http访问 Apache服 ...
- WPF程序将DLL嵌入到EXE的两种方法
WPF程序将DLL嵌入到EXE的两种方法 这一篇可以看作是<Visual Studio 版本转换工具WPF版开源了>的续,关于<Visual Studio 版本转换工具WPF版开源了 ...
- MongoDB实现分页(两种方法)
1.插入实验数据 偷懒用下samus,100条. ; i < ; i++) { Document doc = new Document(); doc["ID"] = i; d ...
- css:图标与文字对齐的两种方法
(好久没写博客了,这几个月的积累比较零碎,记在本子上,现在开始整理归类) 在平时写页面的过程中,常遇到要把小图标与文字对齐的情况.比如: 总结了两种方法,代码量都比较少. 第一种 对img设置竖直方向 ...
随机推荐
- Winform 拦截最小化、最大化、关闭事件【整理】
const int WM_SYSCOMMAND = 0x112; //窗体关闭消息 const int SC_CLOSE = 0xf060; //窗体最小化消息 const int SC_MINIMI ...
- V2EX社区
无论你是在大学进行人生最重要阶段的学习,或者是在中国的某座城市工作,或者是在外太空的某个天体如 Sputnik 1 上享受人生,在注册进入 V2EX 之后,你都可以为自己设置一个所在地,从而找到更多和 ...
- IOS_设置启动图片若干问题
在做项目时, 发现设置了LaunchImage时发现一些问题: 1. 启动图片的设置可以通过两种方法: 1) 通过在LaunchScreen里放入ImageView 并设置图片, 这种方法的好处在于不 ...
- 用 Function.apply() 的参数数组化来提高 JavaScript程序性能
我们再来聊聊Function.apply() 在提升程序性能方面的技巧. 我们先从 Math.max() 函数说起, Math.max后面可以接任意个参数,最后返回所有参数中的最大值. 比如 aler ...
- Odoo 库存管理-库存移动(Stock Move)新玩法
库存移动(Stock Move)新玩法 Odoo的库存移动不仅仅是存货在两个“存货地点”之间的移动的基本概念了,他们可以被“串联”在一起,可以用来生成或改变其对应的拣货单 (Picking).链式库存 ...
- pandas库学习笔记(二)DataFrame入门学习
Pandas基本介绍——DataFrame入门学习 前篇文章中,小生初步介绍pandas库中的Series结构的创建与运算,今天小生继续“死磕自己”为大家介绍pandas库的另一种最为常见的数据结构D ...
- iblog语法高亮示例
-------------------------------------------------------------------------------------- iblog 是一款 Sub ...
- SQL2012 附加数据库提示5120错误解决方法
在win8.1 x64系统上使用sql2012进行附加数据库(包括在x86系统正在使用的数据库文件,直接拷贝附加在X64系统中)时,提示无法打开文件,5120错误. 这个错误是因为没有操作权限,所以附 ...
- oracle 存储过程编辑 卡死
一.可用SYS登录, 二.查锁session_ID查找存储过程OPERATIONDATA_IMP被哪些session锁住而无法编译select * FROM dba_ddl_locks where n ...
- 外企iOS开发的笔试题
一组外企iOS开发的笔试题,您能回答出来吗?从群里收集来的. (miki西游@mikixiyou的文档,原文链接: http://mikixiyou.iteye.com/blog/1546376 转 ...