当前位置: 首页 > news >正文

安阳公司做网站营销推广技巧

安阳公司做网站,营销推广技巧,城市建设杂志社官方网站,什么公司做的网站好44. 通配符匹配 - 力扣(LeetCode) 给你一个输入字符串 (s) 和一个字符模式 (p) ,请你实现一个支持 ? 和 * 匹配规则的通配符匹配: ? 可以匹配任何单个字符。* 可以匹配任意字符序列(包括空字符序列)。 …

44. 通配符匹配 - 力扣(LeetCode)

给你一个输入字符串 (s) 和一个字符模式 (p) ,请你实现一个支持 '?''*' 匹配规则的通配符匹配:

  • '?' 可以匹配任何单个字符。
  • '*' 可以匹配任意字符序列(包括空字符序列)。

判定匹配成功的充要条件是:字符模式必须能够 完全匹配 输入字符串(而不是部分匹配)。

示例 1:

输入:s = "aa", p = "a"
输出:false
解释:"a" 无法匹配 "aa" 整个字符串。

示例 2:

输入:s = "aa", p = "*"
输出:true
解释:'*' 可以匹配任意字符串。

示例 3:

输入:s = "cb", p = "?a"
输出:false
解释:'?' 可以匹配 'c', 但第二个 'a' 无法匹配 'b'。

提示:

  • 0 <= s.length, p.length <= 2000
  • s 仅由小写英文字母组成
  • p 仅由小写英文字母、'?''*' 组成

题解

题目描述:给你两个字符串s文本字符串和p模式字符串,其中p包含两种类型的通配符:

  • *:可以匹配任意字符序列(包括空序列)
  • ?:可以匹配任意单个字符

题目要求你检查sp是否匹配。

Example 1:

  • s = "adceb"
  • p = "*a*b"
  • Output: true
  • Explanation: The first * can match an empty sequence, and the second * matches “dce”.

Example 2:

  • s = "acdcb"
  • p = "a*c?b"
  • Output: false
  • Explanation: There is no way to match s with p, as the character before the last in s is ‘c’ but ‘b’ in p.

这个题用贪心的思路该如何去想呢,在每一步匹配中,我们可以对如何匹配字符串做出局部最优选择,如*,可以表示任何字符序列,包括空序列,可以在匹配字符串的过程中及时调整匹配。

  • 对于?:由于?只匹配一个字符,即自动选择s的当前字符与p中的?匹配。
  • 对于*:因为他可以匹配任意字符序列,所以按照贪心的思路,首先用空序列匹配*,然后,如果需要,根据模式和字符串其余部分扩展或缩小匹配的字符数量。

贪心算法的实现思路

  1. 两个指针+回溯:首先定义两个指针,分别是ssIdx)和ppIdx),进行模式匹配,如果出现不匹配,则回溯到p*的最后一个位置(如果有),并尝试不同的匹配。
  2. ?通配符处理:在迭代中,如果 sp 中的当前字符匹配,或者 p[pIdx]? ,只需将两个指针向前移动。
  3. 处理*通配符:如果p*,需要分别标记*p中的位置(用starIdx),和在s中的对应位置(sTmpIdx表示)。表示*匹配s中的空字符的起点。如果稍后出现不匹配,则回溯到starIdx并增加sTmpIdx,表示*现在应该在s中多匹配一个字符。将pIdx重置为starIdx+1,将 sIdx 重置为 sTmpIdx ,继续匹配。
  4. 最后检查:遍历 s 后,确保 p 中所有剩余的字符都是 * 。如果是,它们可以匹配空序列,因此,模式匹配字符串。

具体示例

Consider :s = "adceb" and p = "*a*b"

Initial Setup

  • s = "adceb"
  • p = "*a*b"
  • sIdx = 0, pIdx = 0, starIdx = -1, sTmpIdx = -1

Iteration 1

  • p[0] is *, so we update starIdx = 0 and sTmpIdx = 0.
  • Increment pIdx to 1.
  • sIdx remains 0.

Iteration 2

  • p[1] is 'a', but s[0] is 'a'. This is a mismatch.
  • Since starIdx != -1, we use the star to match one more character.
  • Increment sTmpIdx to 1. So sTmpIdx = 1.
  • Update pIdx = starIdx + 1, which means pIdx = 1.
  • Update sIdx = sTmpIdx, which means sIdx = 1.

Iteration 3

  • Now p[1] is 'a' and s[1] is also 'a'. This is a match.
  • Increment both sIdx and pIdx by 1.
  • sIdx = 2, pIdx = 2.

Iteration 4

  • p[2] is '*'. Update starIdx = 2 and sTmpIdx = 2.
  • Increment pIdx to 3.
  • sIdx remains 2.

Iteration 5

  • p[3] is 'b', but s[2] is 'd'. This is a mismatch.
  • Since starIdx != -1, we use the star to match one more character.
  • Increment sTmpIdx to 3. So sTmpIdx = 3.
  • Update pIdx = starIdx + 1, which means pIdx = 3.
  • Update sIdx = sTmpIdx, which means sIdx = 3.

Iteration 6

  • p[3] is 'b' and s[3] is 'c'. This is a mismatch.
  • We repeat the backtracking process:
  • Increment sTmpIdx to 4. So sTmpIdx = 4.
  • Update pIdx = starIdx + 1, which means pIdx = 3.
  • Update sIdx = sTmpIdx, which means sIdx = 4.

Iteration 7

  • p[3] is 'b' and s[4] is 'b'. This is a match.
  • Increment both sIdx and pIdx by 1.
  • sIdx = 5, pIdx = 4.

Final Check

  • sIdx is now equal to s.size(), so we exit the while loop.
  • Check remaining characters in p. Since pIdx is also at the end of p, there are no more characters to check.
class Solution {
public:bool isMatch(string s, string p) {int sIdx = 0, pIdx = 0;      // Pointers for s and p.int starIdx = -1, sTmpIdx = -1;  // Indices to track the most recent '*' position in p and the corresponding position in s.while (sIdx < s.size()) {// If the characters match or pattern has '?', move both pointers.if (pIdx < p.size() && (p[pIdx] == s[sIdx] || p[pIdx] == '?')) {++sIdx;++pIdx;}// If pattern has '*', record the position and assume it matches zero characters initially.else if (pIdx < p.size() && p[pIdx] == '*') {starIdx = pIdx;sTmpIdx = sIdx;++pIdx;}// If a mismatch occurs and there was a previous '*', backtrack.// Try to match '*' with one more character in s.else if (starIdx != -1) {pIdx = starIdx + 1;sIdx = ++sTmpIdx;}// If no '*' to backtrack to, return false.else {return false;}}// Check if the remaining characters in pattern are all '*'.// They can match the empty sequence at the end of s.for (; pIdx < p.size(); ++pIdx) {if (p[pIdx] != '*') {return false;}}// If we've processed both strings completely, it's a match.return true;}
};
http://www.dinnco.com/news/62115.html

相关文章:

  • 自助网站制作关键词的作用
  • 郑州建站软件百度网站推广一年多少钱
  • 做网站视频手机数据分析软件工具有哪些
  • 搜狗seo优化镇江网站seo
  • 怎么建立微信群合肥seo搜索优化
  • 广州网站建设 骏域网站建设专家快手流量推广免费网站
  • 商丘专业做网站免费推广网
  • 网站页面配色分析海外网站建站
  • 做网站要素品牌推广软文200字
  • 红酒公司网站建设模板6841免费推广方法
  • 手机编程网站网络营销成功的案例及其原因
  • 西安建站系统代引流推广公司
  • 做海外网站推广免费拓客软件
  • 昆明网站建设流程seo优化网站教程
  • 网站是什么平台seo搜索引擎工具
  • 虎门网站制作网页设计自学要多久
  • 长春微信推广哪个合肥seo好
  • 办公室装修费用会计处理安徽seo顾问服务
  • 山东华建建设有限公司网站网上推广方式
  • 做h的动漫在线观看网站搜索引擎排名2021
  • wordpress查看数据库密码seo交流中心
  • wordpress单用户商城windows优化大师官方网站
  • 建筑企业网站设计宁波seo外包服务平台
  • wordpress设计网页短视频关键词优化
  • 抖音的电子商务网站建设百度关键词关键词大全
  • 大学校园网站模板图片地推的60种方法
  • 一件代发货源app焦作seo公司
  • 做网站注册营业执照苹果cms永久免费建站程序
  • 研究院网站建设方案北京seo教师
  • 外国人做数学视频网站网站服务器地址查询