题目描述:

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两种方法求解的更多相关文章

  1. pat1067 在离散数学中置换群思想上可用并查集和递归两种方法求解问题

    1.递归求解  注:叙述时 节点其实就是数字0-N-1 !!!最好用一个数组记录0-N-1每个数字的位置 !!!递归计算一个置换群内部的节点数 分为两种情况 累加M,M即是一个置换群所有数字在正确位置 ...

  2. HDU 1160 排序或者通过最短路两种方法解决

    题目大意: 给定一堆点,具有x,y两个值 找到一组最多的序列,保证点由前到后,x严格上升,y严格下降,并把最大的数目和这一组根据点的编号输出来 这里用两种方法来求解: 1. 我们可以一开始就将数组根据 ...

  3. windows下获取IP地址的两种方法

    windows下获取IP地址的两种方法: 一种可以获取IPv4和IPv6,但是需要WSAStartup: 一种只能取到IPv4,但是不需要WSAStartup: 如下: 方法一:(可以获取IPv4和I ...

  4. android 之 启动画面的两种方法

    现在,当我们打开任意的一个app时,其中的大部分都会显示一个启动界面,展示本公司的logo和当前的版本,有的则直接把广告放到了上面.启动画面的可以分为两种设置方式:一种是两个Activity实现,和一 ...

  5. [转载]C#读写txt文件的两种方法介绍

    C#读写txt文件的两种方法介绍 by 大龙哥 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char ...

  6. php如何防止图片盗用/盗链的两种方法(转)

    图片防盗链有什么用? 防止其它网站盗用你的图片,浪费你宝贵的流量.本文章向大家介绍php防止图片盗用/盗链的两种方法 Apache图片重定向方法 设置images目录不充许http访问 Apache服 ...

  7. WPF程序将DLL嵌入到EXE的两种方法

    WPF程序将DLL嵌入到EXE的两种方法 这一篇可以看作是<Visual Studio 版本转换工具WPF版开源了>的续,关于<Visual Studio 版本转换工具WPF版开源了 ...

  8. MongoDB实现分页(两种方法)

    1.插入实验数据 偷懒用下samus,100条. ; i < ; i++) { Document doc = new Document(); doc["ID"] = i; d ...

  9. css:图标与文字对齐的两种方法

    (好久没写博客了,这几个月的积累比较零碎,记在本子上,现在开始整理归类) 在平时写页面的过程中,常遇到要把小图标与文字对齐的情况.比如: 总结了两种方法,代码量都比较少. 第一种 对img设置竖直方向 ...

随机推荐

  1. linux解压zip、bz、bz2、z、gz、tar(解包)

    zip: 压缩: zip [-AcdDfFghjJKlLmoqrSTuvVwXyz$][-b <工作目录>][-ll][-n <字尾字符串>][-t <日期时间>] ...

  2. JNI 学习笔记

    JNI是Java Native Interface的缩写,JNI是一种机制,有了它就可以在java程序中调用其他native代码,或者使native代码调用java层的代码.也 就是说,有了JNI我们 ...

  3. Python解析HTML的开发库pyquery

    PyQuery是一个类似于jQuery的Python库,也可以说是jQuery在Python上的实现,能够以 jQuery 的语法来操作解析 HTML 文档,易用性和解析速度都很好. 例如,一段豆瓣h ...

  4. c#中的类型转换

    Parse类型转换 Parse()函数 int.double都能调用Parse()函数,Parse(string str);如果转换成功就成功,失败就会抛出一个异常; TryParse()函数 相应地 ...

  5. vs2012用wpf制作透明窗口中报错的解决方案

    在开发wpf项目时,需要调用外部com组件,同时需要制作透明窗口,于是问题出现了,当我们在设置 AllowsTransparency="True"后,com组件显示不出来了,只有透 ...

  6. redis 界面软件使用

    ubuntu 下下载安装包 sudo dpkg -i redis-desktop-manager_0.8.3-120_amd64.deb//安装 redis-desktop-manager //启动

  7. 【开发记录】iOS中使用 Reachability 检测网络

    如果你想在iOS程序中提供一仅在wifi网络下使用(Reeder),或者在没有网络状态下提供离线模式(Evernote).那么你会使用到Reachability来实现网络检测. 写本文的目的 了解Re ...

  8. SVN的405错误

    错误1: 如果你没有阅读以下文字,活该你倒霉(这段文字是在googlecode添加新项目时生成的): Command-line access If you plan to make changes, ...

  9. PVPGN 暗黑破坏神2 1.11b战网配置问题汇总

    写了第一篇配置指南之后,很多人向我咨询有关战网搭建的问题.于是觉得很有必要把若干常见的问题,和常用的进阶配置汇总一下,以方便更多人. 1.游戏版本和PVPGN与D2GS版本的问题. PVPGN建议选择 ...

  10. FineUI Grid控件右键菜单的实现

    FineUI官方Demo上一直没有Grid右键菜单的实现,其实从4.1.x的版本开始,允许添加自定义的事件监听(Listeners),所以要实现这个功能已经相当容易了. ExtJs右键菜单有很多种,对 ...