【 題目】
請實現乙個函式用來匹配包含』.『和』『的正規表示式。模式中的字元』.』
表示任意乙個字元,而』'表示它前面的字元可以出現任意次(含0次)。在本題
中,匹配是指字串的所有字元匹配整個模式。例如,字串"aaa"與模式"a.a"
和"abaca"匹配,但與"aa.a"及"ab*a"均不匹配。
【輸入輸出】
test
("test01",""
,"",true);
test
("test02",""
,".*"
,true);
test
("test03",""
,"."
,false);
test
("test04",""
,"c*"
,true);
test
("test05"
,"a"
,".*"
,true);
test
("test06"
,"a"
,"a."
,false);
test
("test07"
,"a",""
,false);
test
("test08"
,"a"
,"."
,true);
test
("test09"
,"a"
,"ab*"
,true);
test
("test10"
,"a"
,"ab*a"
,false);
test
("test11"
,"aa"
,"aa"
,true);
test
("test12"
,"aa"
,"a*"
,true);
test
("test13"
,"aa"
,".*"
,true);
test
("test14"
,"aa"
,"."
,false);
test
("test15"
,"ab"
,".*"
,true);
test
("test16"
,"ab"
,".*"
,true);
test
("test17"
,"aaa"
,"aa*"
,true);
test
("test18"
,"aaa"
,"aa.a"
,false);
test
("test19"
,"aaa"
,"a.a"
,true);
test
("test20"
,"aaa"
,".a"
,false);
test
("test21"
,"aaa"
,"a*a"
,true);
test
("test22"
,"aaa"
,"ab*a"
,false);
test
("test23"
,"aaa"
,"ab*ac*a"
,true);
test
("test24"
,"aaa"
,"ab*a*c*a"
,true);
test
("test25"
,"aaa"
,".*"
,true);
test
("test26"
,"aab"
,"c*a*b"
,true);
test
("test27"
,"aaca"
,"ab*a*c*a"
,true);
test
("test28"
,"aaba"
,"ab*a*c*a"
,false);
test
("test29"
,"bbbba"
,".*a*a"
,true);
test
("test30"
,"bcbbabab"
,".*a*a"
,false
);
【參考**】
#include
//#include
#include
//#include
using
namespace std;
bool
matchcore
(const
char
* str,
const
char
* pattern)if(
*(pattern+1)
=='*'
)else}if
(*str ==
*pattern ||
(*pattern ==
'.'&&
*str !=
'\0'))
return
false;}
bool
match
(const
char
*str,
const
char
*pattern)
return
matchcore
(str, pattern);}
intmain()
/****
[give|show]me [the](money|cash)
[show)me[the](money|cash)
( money | cash)
( | money)10
00***/
面試題19正規表示式匹配
題目 實現乙個函式用來匹配包含 和 的正規表示式,可以代表任意字母,表示他前面的字元可以出現 0次,includeusing namespace std bool matchcore char str,char pattern if str 0 pattern 0 if str 0 pattern ...
面試題19 正規表示式匹配
請實現乙個函式用來匹配包含 和 的正規表示式。模式中的字元 表示任意乙個字元,而 表示它前面的字元可以出現任意次 含0次 匹配是指字串的所有字元匹配整個模式。例如,字串 aaa 與模式 a.a 和 ab ac a 匹配,但與 aa.a 和 ab a 均不匹配。每次從字串裡拿出乙個字元和模式中的字元去...
面試題19 正規表示式匹配
題目 請實現乙個函式用來匹配包含 和 的正規表示式。其中 表示匹配任意字元,表示匹配0次或則多次,本題中,匹配是指字串的全匹配。分析 由於是全匹配,我們可以給字串和模式串設定兩個指標,指向當前的要匹配的字元。有以下幾種情況 基於以上的幾種情況,採用遞迴的方式完成 的編寫比較簡潔 include us...