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;
//}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章