Problem:
Implement regular expression matching with support for '. ' and ' * '. '. ' Matches any single character. ' * ' Matches zero or more of The<span style= ' color: #ff0000; " > preceding element</span>. 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") →falseismatch ( "AA", "AA") →trueismatch ("AAA", "AA") →falseismatch ("AA", "A *") →trueismatch ("AA", ". *") →trueismatch ("AB", ". *") →true IsMatch ("AaB", "C*a*b") →true
Thinking:
A few words of the Spit Groove:
String match, '. ' And the meaning of ' * ' is very simple, without stating, but:
IsMatch ("AaB", "C*a*b") →true
What kind of thing?
Leetcode (http://articles.leetcode.com/2011/09/regular-expression-matching.html) has a lot of people arguing, this c* is not can represent 0 C, I TM no language.
The final feeling is that the problem is to promote that particular algorithm and change the conditions?
PS: Read the wrong question ..... 2 B.
(1) without * The situation is very good to solve, difficult to deal with
(2) such as ABBC and A*c, A*BC, very clear, the use of deep search ideas, as long as the BB and *, *b the whole to match on the OK
I wrote a test program, and the official one--that's a lame-out condition.
#include <iostream> #include <memory.h>using namespace Std;class solution {Public:bool isMatch (const char * s, const char *p) {int n=0; int m=0; int index=0; while (* (s+n)! = ') ') {n++; } while (* (p+m)! = ') ') {m++; }//cout<< "N:" <<n<< "M:" <<m<<endl; if (n==0| | M==0) return false; int *a = new Int[m]; memset (a,0,sizeof (int) *m); if ((*p== '. ') | | (*p==*s)) {a[0]=1; index++; } for (int i=1;i<m;i++) {if (a[i]==1) continue; while (index<n) {if (* (p+i) = = '. ') {a[i]=a[i-1]&0x01; index++; } else if (* (p+i) = = ' * ') {int tmp_s=1; int tmp_p=1; while ((* (* (s+index) ==* (s+index+tmp_s)) && (index+tmp_s<n)) {tmp_s++; } while ((* (S+index) ==* (p+i+tmp_p)) && (i+tmp_p<m)) { tmp_p++; } if ((index+tmp_s==n-1) && (i+tmp_p==m-1)) {a[i+tmp_p] =a[i-1]&0x01; index+=tmp_s; Break } else {for (int j=0;j<tmp_p;j++) { a[i+j]=a[i+j-1]&0x01; index+=tmp_s; }}//else}//else If else {if (* (S+inde x) ==* (p+i)) {index++; a[i]=a[i-1]&0x01; cout<< "I:" <<i<< "a[i": "<<a[i]<<endl; } else {a[i]=a[i-1]&0x00; return false; }}//else if (i==m-1) break; }//while}//for if (index<n) return false; else return a[m-1]; }//ismatch};int Main () {char *s= "AAB"; Char *p= "AA"; Solution MySolution; Cout<<mysolution.ismatch (S,p) <<endl;}
I tested a few groups, passed, welcome to find bugs.
Leetcode | | Regular Expression Matching problem