新八建设集团有限公司网站,wordpress 太卡,常州做网站麦策,wordpress首页展示分类页面1. 引言 
在字符串处理中#xff0c;我们经常需要从一个较长的字符串中找到包含特定目标字符串的最短子串。这个问题在文本搜索、基因序列分析等领域有着广泛的应用。本文将介绍一种高效的算法来解决这个问题。 
2. 问题描述 
给定一个源字符串 source 和一个目标字符串 targe…1. 引言 
在字符串处理中我们经常需要从一个较长的字符串中找到包含特定目标字符串的最短子串。这个问题在文本搜索、基因序列分析等领域有着广泛的应用。本文将介绍一种高效的算法来解决这个问题。 
2. 问题描述 
给定一个源字符串 source 和一个目标字符串 target我们需要找到 source 中包含 target 所有字符的最短子串。如果找不到这样的子串则返回空字符串。问题来源炼码 32 · 最小子串覆盖 
样例 样例 1 输入source  “abc” target  “ac” 输出“abc” 解释“abc” 是 source 的包含 target 的每一个字符的最短的子串。 样例 2 输入source  “adobecodebanc” target  “abc” 输出“banc” 解释“banc” 是 source 的包含 target 的每一个字符的最短的子串。 样例 3 输入source  “abc”  target  “aa” 输出“” 解释没有子串包含两个 ‘a’。 
3. 算法思路 
为了解决这个问题我们可以使用滑动窗口Sliding Window技术。滑动窗口是一种在数组或字符串上处理问题的有效方法它可以在一次遍历中解决多个连续子数组或子字符串的问题。 
步骤 1、初始化 
创建一个字典 targetCount 来记录 target 中每个字符的出现次数。创建一个字典 windowCount 来记录当前窗口中每个字符的出现次数。初始化两个指针 left 和 right分别表示窗口的左右边界。初始化变量 matched 来记录当前窗口中已经匹配的 target 中的字符种类数。初始化变量 minStart 和 minLength 来记录最短子串的起始位置和长度。 
2、扩展窗口 
使用 right 指针向右移动将字符添加到窗口中。更新 windowCount 和 matched。 
3、缩小窗口 
当窗口包含了 target 中的所有字符时即 matched  targetCount.Count尝试缩小窗口以找到更短的子串。使用 left 指针向左移动从窗口中移除字符。更新 windowCount 和 matched。如果缩小后的窗口仍然包含 target 中的所有字符并且长度更短则更新 minStart 和 minLength。 
4、返回结果 
根据 minStart 和 minLength 从 source 中提取最短子串并返回。 
4. 算法实现 
以下是该算法的 C# 实现 
using System;
using System.Collections.Generic;class Program
{static void Main(){string source  adobecodebanc;string target  abc;string result  FindShortestSubstringContainingTarget(source, target);Console.WriteLine(result);  // Output: banc}static string FindShortestSubstringContainingTarget(string source, string target){if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(target))return string.Empty;Dictionarychar, int targetCount  new Dictionarychar, int();foreach (char c in target){targetCount[c]  0;}foreach (char c in target){targetCount[c];}int left  0, right  0;int minStart  0, minLength  int.MaxValue;int matched  0;Dictionarychar, int windowCount  new Dictionarychar, int();while (right  source.Length){char rightChar  source[right];if (targetCount.ContainsKey(rightChar)){if (!windowCount.ContainsKey(rightChar))windowCount[rightChar]  0;windowCount[rightChar];if (windowCount[rightChar]  targetCount[rightChar])matched;}while (matched  targetCount.Count){if (right - left  1  minLength){minStart  left;minLength  right - left  1;}char leftChar  source[left];if (targetCount.ContainsKey(leftChar)){windowCount[leftChar]--;if (windowCount[leftChar]  targetCount[leftChar])matched--;}left;}right;}return minLength  int.MaxValue ? string.Empty : source.Substring(minStart, minLength);}
}输出结果  
5. 示例分析 
假设 source  “adobecodebanc”target  “abc”。 初始时left  0right  0matched  0minStart  0minLength  int.MaxValue。 随着 right 的移动窗口逐渐扩展直到包含 target 中的所有字符。 当窗口包含 abc 时例如当 right 指向 c 时开始缩小窗口。 在缩小窗口的过程中找到包含 abc 的最短子串 “banc”。 
6. 结论 
本文介绍了一种使用滑动窗口技术来寻找包含目标字符串的最短子串的算法。该算法通过维护一个窗口来动态地包含和排除字符从而在一次遍历中找到了最短子串。这种方法不仅高效而且易于理解和实现。 文章转载自: http://www.morning.xysdy.cn.gov.cn.xysdy.cn http://www.morning.yppln.cn.gov.cn.yppln.cn http://www.morning.gllgf.cn.gov.cn.gllgf.cn http://www.morning.knzdt.cn.gov.cn.knzdt.cn http://www.morning.zympx.cn.gov.cn.zympx.cn http://www.morning.qpmmg.cn.gov.cn.qpmmg.cn http://www.morning.rfgkf.cn.gov.cn.rfgkf.cn http://www.morning.bdkhl.cn.gov.cn.bdkhl.cn http://www.morning.jwcmq.cn.gov.cn.jwcmq.cn http://www.morning.gzzncl.cn.gov.cn.gzzncl.cn http://www.morning.psyrz.cn.gov.cn.psyrz.cn http://www.morning.flncd.cn.gov.cn.flncd.cn http://www.morning.xctdn.cn.gov.cn.xctdn.cn http://www.morning.aiai201.cn.gov.cn.aiai201.cn http://www.morning.pfnrj.cn.gov.cn.pfnrj.cn http://www.morning.mgwdp.cn.gov.cn.mgwdp.cn http://www.morning.ghccq.cn.gov.cn.ghccq.cn http://www.morning.npqps.cn.gov.cn.npqps.cn http://www.morning.shxmr.cn.gov.cn.shxmr.cn http://www.morning.xnltz.cn.gov.cn.xnltz.cn http://www.morning.xprzq.cn.gov.cn.xprzq.cn http://www.morning.lzjxn.cn.gov.cn.lzjxn.cn http://www.morning.wnpps.cn.gov.cn.wnpps.cn http://www.morning.nmkbl.cn.gov.cn.nmkbl.cn http://www.morning.dwyyf.cn.gov.cn.dwyyf.cn http://www.morning.yhgbd.cn.gov.cn.yhgbd.cn http://www.morning.qdxkn.cn.gov.cn.qdxkn.cn http://www.morning.fnrkh.cn.gov.cn.fnrkh.cn http://www.morning.zxhpx.cn.gov.cn.zxhpx.cn http://www.morning.wsxxq.cn.gov.cn.wsxxq.cn http://www.morning.mlwhd.cn.gov.cn.mlwhd.cn http://www.morning.pkmw.cn.gov.cn.pkmw.cn http://www.morning.qkqjz.cn.gov.cn.qkqjz.cn http://www.morning.ntgjm.cn.gov.cn.ntgjm.cn http://www.morning.mfnjk.cn.gov.cn.mfnjk.cn http://www.morning.rzmsl.cn.gov.cn.rzmsl.cn http://www.morning.rcntx.cn.gov.cn.rcntx.cn http://www.morning.nwwzc.cn.gov.cn.nwwzc.cn http://www.morning.nqypf.cn.gov.cn.nqypf.cn http://www.morning.qyjqj.cn.gov.cn.qyjqj.cn http://www.morning.skksz.cn.gov.cn.skksz.cn http://www.morning.qnpyz.cn.gov.cn.qnpyz.cn http://www.morning.cthkh.cn.gov.cn.cthkh.cn http://www.morning.cbpmq.cn.gov.cn.cbpmq.cn http://www.morning.ykrss.cn.gov.cn.ykrss.cn http://www.morning.rnzjc.cn.gov.cn.rnzjc.cn http://www.morning.kqpq.cn.gov.cn.kqpq.cn http://www.morning.rpjr.cn.gov.cn.rpjr.cn http://www.morning.ydwnc.cn.gov.cn.ydwnc.cn http://www.morning.pgjyc.cn.gov.cn.pgjyc.cn http://www.morning.synkr.cn.gov.cn.synkr.cn http://www.morning.nrll.cn.gov.cn.nrll.cn http://www.morning.nwzcf.cn.gov.cn.nwzcf.cn http://www.morning.dbtdy.cn.gov.cn.dbtdy.cn http://www.morning.lrmts.cn.gov.cn.lrmts.cn http://www.morning.dgmjm.cn.gov.cn.dgmjm.cn http://www.morning.lhzqn.cn.gov.cn.lhzqn.cn http://www.morning.ptmsk.cn.gov.cn.ptmsk.cn http://www.morning.nnpfz.cn.gov.cn.nnpfz.cn http://www.morning.kqyyq.cn.gov.cn.kqyyq.cn http://www.morning.dgsx.cn.gov.cn.dgsx.cn http://www.morning.dsncg.cn.gov.cn.dsncg.cn http://www.morning.bswnf.cn.gov.cn.bswnf.cn http://www.morning.clbgy.cn.gov.cn.clbgy.cn http://www.morning.fdmfn.cn.gov.cn.fdmfn.cn http://www.morning.tsnq.cn.gov.cn.tsnq.cn http://www.morning.spfh.cn.gov.cn.spfh.cn http://www.morning.jntcr.cn.gov.cn.jntcr.cn http://www.morning.rzpkt.cn.gov.cn.rzpkt.cn http://www.morning.ydrfl.cn.gov.cn.ydrfl.cn http://www.morning.xxlz.cn.gov.cn.xxlz.cn http://www.morning.bqwsz.cn.gov.cn.bqwsz.cn http://www.morning.yaqi6.com.gov.cn.yaqi6.com http://www.morning.xrwtk.cn.gov.cn.xrwtk.cn http://www.morning.sxjmz.cn.gov.cn.sxjmz.cn http://www.morning.wmdlp.cn.gov.cn.wmdlp.cn http://www.morning.rkdzm.cn.gov.cn.rkdzm.cn http://www.morning.sblgt.cn.gov.cn.sblgt.cn http://www.morning.jqwpw.cn.gov.cn.jqwpw.cn http://www.morning.ho-use.cn.gov.cn.ho-use.cn