日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

LeetCode Regular Expression Matching

發布時間:2025/6/15 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LeetCode Regular Expression Matching 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


‘.’ Matches any single character.
‘*’ Matches zero or more of the preceding element.

The matching should cover the entire input string (not partial).

The function prototype should be:
bool isMatch(const char *s, const char *p)

Some examples:
isMatch(“aa”,”a”) → false
isMatch(“aa”,”aa”) → true
isMatch(“aaa”,”aa”) → false
isMatch(“aa”, “a*”) → true
isMatch(“aa”, “.*”) → true
isMatch(“ab”, “.*”) → true
isMatch(“aab”, “c*a*b”) → true


多階段決策過程的最優化問題

子問題的重疊性

p[i][j]? 表示字串 s[i...len(s)], p[j...len(p)] 是否可以匹配。

遞歸地定義最優值

如果P[j+1]!='*',S[i] == P[j]=>匹配下一位(i+1, j+1),S[i]!=P[j]=>匹配失敗;

如果P[j+1]=='*',S[i]==P[j]=>匹配下一位(i+1, j+2)或者(i, j+2),S[i]!=P[j]=>匹配下一位(i,j+2)。

匹配成功的條件為S[i]=='\0' && P[j]=='\0'。

dp[i][j] =?c1. p[j+1] != *. ? if s[i] == p[j] ?dp[i][j] = dp[i+1][j+1]?????? else dp[i][j] = false?? ? ? ? ? ? ??

c2 p[j+1] == '*'??????? ?if( s[i] == ?p[j] || p[j] == '.' && (*s) != 0)?

p[j] == . ?因為他可以匹配任何字符,所以和相等關系有基本一樣的方式。


(1)p[j+1]不是'*'。情況比較簡單,只要判斷如果當前s的i和p的j上的字符一樣(如果有p在j上的字符是'.',也是相同),并且res[i][j]==true,則res[i+1][j+1]也為true,res[i+1][j+1]=false;?
(2)p[j+1]是'*',但是p[j]!='.'。那么只要以下條件有一個滿足即可對res[i+1][j+1]賦值為true:?
? ? 1)res[i+1][j]為真('*'只取前面字符一次);?
? ? 2)res[i+1][j-1]為真('*'前面字符一次都不取,也就是忽略這兩個字符);?
? ? 3)res[i][j+1] && s[i]==s[i-1] && s[i-1]==p[j-1](這種情況是相當于i從0到s.length()掃過來,如果p[j+1]對應的字符是‘*’那就意味著接下來的串就可以依次匹配下來,如果下面的字符一直重復,并且就是‘*’前面的那個字符)。?
(3)p[j+1]是'*',并且p[j]=='.'。因為".*"可以匹配任意字符串,所以在前面的res[i+1][j-1]或者res[i+1][j]中只要有i+1是true,那么剩下的res[i+1][j+1],res[i+2][j+1],...,res[s.length()][j+1]就都是true了。?
這道題有個很重要的點,就是實現的時候外層循環應該是p,然后待匹配串s內層循環掃過來

res[][]?保持中間計算值

/** regular.cpp** Created on: 2014年12月26日* Author: judyge*/ #include <iostream> #include <string> #include<stdio.h> #include<stdlib.h> #include<time.h> #include<windows.h> using namespace std;bool isMatch(string s, string p) {if(s.length()==0 && p.length()==0)return true;if(p.length()==0)return false;bool res[s.length()+1][p.length()+1];res[0][0] = true;for(int j=0;j<p.length();j++){if(p[j]=='*'){if(j>0 && res[0][j-1]) res[0][j+1]=true;if(j<1) continue;if(p[j-1]!='.'){for(int i=0;i<s.length();i++){if(res[i+1][j] || j>0&&res[i+1][j-1]|| i>0 && j>0 && res[i][j+1]&&s[i]==s[i-1]&&s[i-1]==p[j-1])res[i+1][j+1] = true;}}else{int i=0;while(j>0 && i<s.length() && !res[i+1][j-1] && !res[i+1][j])i++;for(;i<s.length();i++){res[i+1][j+1] = true;}}}else{for(int i=0;i<s.length();i++){if(s[i]==p[j] || p[j]=='.')res[i+1][j+1] = res[i][j];}}}return res[s.length()][p.length()]; }int main(){char *s="aab";char *p="c*a*b";char *m="aaa";char *n="aa";clock_t start,finish;double time;start=clock();cout<<isMatch(s,p)<<"\n";cout<<isMatch(m,n)<<"\n";finish=clock();time=(double)((finish-start)/CLOCKS_PER_SEC);printf("start:%ld\t\tfinish:%ld\tfinish-start:%ld\truntime:%f\n",start,finish,finish-start,time);return 0;}運行結果


1 0 start:1 finish:1 finish-start:0 runtime:0.000000

總結

以上是生活随笔為你收集整理的LeetCode Regular Expression Matching的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。