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设置竖直方向 ...
随机推荐
- HMTL5的 video 在IOS7中碰到的坑
直接说问题吧, 测试设备,ipod 我们在移动端播放视频的时候,一般使用H5的video标签,OK,这里有几点差异(就我目前所发现的)给大家分享一下, 1.在IOS7中,video元素是需要确定大小的 ...
- 无法安装程序包“MIcrosoft.Owin.Security 2.0.2”。您正在尝试将此程序包安装到某个将“.NETFramework,Version=v4.0”作为目标的项目中。
在VS2010 MVC4项目中,安装NuGet程序包Microsoft.AspNet.SignalR时出现以下错误: 原因是安装的版本是Microsoft.AspNet.SignalR 2.0.2,要 ...
- RHEL7 添加用户,含sudo权限
1.添加普通用户[root@server ~]# useradd book //添加一个名为book的用户 [root@server ~]# passwd book //修改密码 Changing p ...
- php 文件上传一例简单代码
1.程序文件 <?php //判断临时文件存放路径是否包含用户上传的文件 if(is_uploaded_file($_FILES["uploadfile"]["tm ...
- 深入解析PHP 5.3.x 的strtotime() 时区设定 警告信息修复
在某些参考资料中是说这两个方法任选其一就可,但经我测试,必须两个方法同时使用,才不会再出现错误提示 PHP Warning: strtotime(): It is not safe to rely o ...
- Python开发【第一篇】Python基础之反射
反射 反射的作用:反射得作用是提高代码可读行. __import__导入模块和import导入模块的区别: __import__导入模块是通过字符串进行导入. import是常用得导入模块方法. 扩展 ...
- asp.net 生成PDF方法
今天转博客园看到有人发表了一篇生成PFd的文章,准备自己也留一份准备以后用到的时候方便调用: 首先去itextsharp网站下载控件(https://sourceforge.net/projects/ ...
- Hive表分区
必须在表定义时创建partition a.单分区建表语句:create table day_table (id int, content string) partitioned by (dt stri ...
- 仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表中的标识列指定显式值
今天在处理数据时遇到这样一个错误 消息 8101,级别 16,状态 1,第 1 行 仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'dbo.StockDetailValu ...
- AFNetwork作用和用法详解
AFNetwork是一个轻量级的网络请求api类库.是以NSURLConnection, NSOperation和其他方法为基础的. 下面这个例子是用来处理json请求的:NSURL *url = [ ...