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设置竖直方向 ...
随机推荐
- visual studio中创建单元测试
1 打开 工具--自定义 2 选择 上下文菜单--编辑器上下文菜单|代码窗口 3 在这里我们可以看到“创建单元测试”这个菜单了,将它移到运行测试菜单下面 4 关闭VS并重启 重启后再对着类名,点击右 ...
- 【PHP】phpcms html去除空白
// 文件路径:/phpcms/libs/classes/template_cache.class.php 42行 // 第四第五行是新增的 $content = $this->template ...
- Oracle bbed使用说明1
一.centos上编译安装BBED工具 [orasrv@localhost ~]$ cd $ORACLE_HOME/rdbms/lib [orasrv@localhost ~]$ make -f in ...
- linux文件的通用操作方法学习
2014-07-29 23:36:10 在linux下用文件描述符来表示设备文件和普通文件.文件描述符是一个整型的数据,所有对文件的操作都通过文件描述符实现. 文件描述符示文件系统中连接用户空间和内核 ...
- Tools for Presention
ZoomIt v4.5 http://technet.microsoft.com/en-us/sysinternals/bb897434.aspx 微软的教师演示工具 主要有放大,画图,倒计时的功能. ...
- Java 图形编程 二:布局管理器之顺序布局
package second; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.Window ...
- ios按钮点击后翻转效果
代码是网上找到的,不过找到的时候直接复制下来不能用,稍微整理下,为和我一样水平的菜鸟观摩一下下. (1)引入“QuartzCore.framework”库,头部引用. #include<Quar ...
- mysql存储过程中传decimal值会自动四舍五入,没有小数
通过 call proc(0.2,0.5); 查看结果数据库竟然是0 和 1 原因:proc的参数没有设置好 参数:原本是 in a decimal,in b decimal 应该改为:in ...
- PVPGN 暗黑破坏神2 1.11b战网配置问题汇总
写了第一篇配置指南之后,很多人向我咨询有关战网搭建的问题.于是觉得很有必要把若干常见的问题,和常用的进阶配置汇总一下,以方便更多人. 1.游戏版本和PVPGN与D2GS版本的问题. PVPGN建议选择 ...
- c++ read
#include <fstream> #include <iostream> int main(void) { ] = {}; ; std::ifstream read(&qu ...