资讯动态

154 · 正则表达式匹配(双序列dp)

发布时间:2026/10/27 1:15:00 来源:尧图企业网站定制
链接LintCode 炼码 - 更高效的学习体验题解class Solution { public: bool isMatch(string s, string p) { int m s.size(); int n p.size(); // 使用 vectorvectorint-1 表示未计算0 表示 false1 表示 true vectorvectorint memo(m 1, vectorint(n 1, -1)); return search(0, 0, s, p, memo) 1; } private: int search(int i, int j, const string s, const string p, vectorvectorint memo) { // 如果已经计算过直接返回 if (memo[i][j] ! -1) { return memo[i][j]; } int ans; if (j (int)p.size()) { // 模式串用完字符串也必须用完 ans (i (int)s.size()) ? 1 : 0; } else { // 当前字符是否匹配注意 i s.size() 的判断 bool firstMatch (i (int)s.size() (s[i] p[j] || p[j] .)); if (j 1 (int)p.size() p[j 1] *) { // 两种情况* 匹配 0 次或匹配 1 次后继续用 * // 注意这里没有在 i s.size() 时提前返回而是让 firstMatch false bool zero (search(i, j 2, s, p, memo) 1); bool more (firstMatch search(i 1, j, s, p, memo) 1); ans (zero || more) ? 1 : 0; } else { // 普通字符匹配必须匹配成功且后续也成功 ans (firstMatch search(i 1, j 1, s, p, memo) 1) ? 1 : 0; } } memo[i][j] ans; return ans; } };class Solution { public: /** * param s: A string * param p: A string includes . and * * return: A boolean */ bool isMatch(string s, string p) { // write your code here int m s.size(); int n p.size(); if (m 0 n 0) { return true; } std::vectorstd::vectorint dp(m1, std::vectorint(n1, false)); for (int i 0; i m; i) { for (int j 0; j n; j) { if (i 0 j 0) { dp[i][j] true; } else if (j 0) { } else if (i 0 p[j-1] .) { // 当前.匹配上字符 dp[i][j] | dp[i-1][j-1]; } else if (p[j-1] *) { // *表示匹配0前面字符情况s[i-1] 和p[j-3]前面匹配决定 dp[i][j] | dp[i][j-2]; // 把s[i-1] 和 p[j-2] 判断是否匹配当作一个整体 if (i 0 j 1 (p[j-2] s[i-1] || p[j-2] .)) { dp[i][j] | dp[i-1][j]; } } else if (s[i-1] p[j-1]) { // 如果字符相等 dp[i][j] | dp[i-1][j-1]; } } } return dp[m][n]; } };class Solution { public: /** * param s: A string * param p: A string includes . and * * return: A boolean */ std::vectorstd::vectorint _dp; bool isMatch(string s, string p) { // write your code here int m s.size(); int n p.size(); if (m 0 n 0) { return true; } if (n 0) { return false; } _dp.resize(m, std::vectorint(n, -1)); return dfs(s, p, 0, 0); } private: bool dfs(string s, string p, int s1, int p1) { if (s1 s.size()) { if (p1 p.size()) { return true; } if (p[p1] * p1 1 p.size()) { return true; } if (p1 1 p.size() p[p11] *) { if (dfs(s, p, s1, p12)) { return true; } } return false; } if (_dp[s1][p1] ! -1) { return _dp[s1][p1] 1 ? true : false; } if (s[s1] p[p1]) { if(dfs(s, p, s11, p11)) { _dp[s1][p1] 1; return true; } if (p1 1 p.size() p[p11] *) { return dfs(s, p, s11, p12) || dfs(s, p, s1, p12); } } else if (p[p1] .) { if(dfs(s, p, s11, p11)) { return true; } if (p1 1 p.size() p[p11] *) { cout test endl; _dp[s1][p1] (dfs(s, p, s11, p1) || dfs(s, p, s11, p12) || dfs(s, p, s1, p12)) true ? 1 : 0; return _dp[s1][p1] 1 ? true : false; } } else if (p[p1] *) { if (dfs(s, p, s1, p11)) { return true; } if (p1 0 (s[s1] p[p1-1] || p[p1-1] .)) { cout haha endl; if (dfs(s, p, s11, p1)) { return true; } if (dfs(s, p, s11, p11)) { return true; } } } else if (p1 1 p.size() p[p11] *) { //cout okok endl; _dp[s1][p1] dfs(s, p, s1, p12) true ? 1 : 0; return _dp[s1][p1] 1 ? true : false; } _dp[s1][p1] 0; return false; } };class Solution { public boolean isMatch(String s, String p) { return search(0, 0,s,p, new Boolean[s.length() 1][p.length() 1]); } private boolean search(int i, int j, String s, String p, Boolean[][] memo){ if(memo[i][j] ! null){ return memo[i][j]; } boolean ans; if(j p.length()){ ans i s.length(); }else{ boolean firstMatch (i s.length() (s.charAt(i) p.charAt(j) || p.charAt(j) .)); if(j 1 p.length() p.charAt(j 1) *){ ans (search(i,j 2, s, p, memo) || firstMatch search(i 1,j, s, p, memo)); }else{ ans firstMatch search(i 1,j 1, s, p, memo); } } memo[i][j] ans; return ans; } }

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价 →
↑