Problem Description
Topic links
‘.’ 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
Problem Analysis
The regular expression matching problem, first of all to clarify the requirements of the problem, summed down in the regular formula will have the following four types of regular items:
- Type A, single letter, normal match, 1 to 1;
- A * type that matches 0 to n repeating letters (a *), depending on the situation;
- . type, matching any 1 letters;
- . * Type, cock explosion exists, want to match yourself!
Algorithmic ideation
Didn't quite understand at first. the meaning of the detour, drilled the horn, has always wanted to match from the end of the string to the middle, and finally, the. *xxx. Pattern, according to this assumption on the Leetcode test given the situation of wrong answer, the problem is that XXX may appear in the string more than 1 times! And I willful take it appears "first time", so will leak to some situation, cause can not match.
Later thought to solve this problem must be from the front to the back of the matching process (think more will have this awareness), the key is how to solve a * and. * Type of match, well, very simple (now feel ╮(╯▽╰)╭), is to put all possible answers as candidate sets, for example:a Met AAA, There are four possibilities: 0 matches, a,aa,aaa, and *aaa encounters AAA There are three possible: a,aa,aaa;.* encounter BCD, there are four alternates: 0 matches, B,BC,BCD.
Data Structure
Here's the problem, duang,duang,duang~.
-Preprocessing regular expressions?
Preprocessing a string of regular expressions into the four types of regular expressions
-How do you manage these waitlist sets?
Queue, well, each time a new match is placed in the queue.
-How to tell if the match is successful?
In both cases, the match string and the regular expression all match the end, it must be successful, and the other is only to match the character to the end of the match and the rest of the regular expression is a * or. * The regular term of the type.
Code
Description: Starting with the vector storage of the standby set, the run time is 73ms, and then the array of the loop queue (note: Here does not consider the situation of the queue full, so the array opened very large) after shortening to 27ms, improve the effect is significant ...
classSolution { Public:BOOLIsMatch (Const Char*s,Const Char*P) {intSlen =strlen(s);intPlen =strlen(p);intAlen =0;inti =0, j =0;intCount_p_single =0;string*arr =New string[Plen];//Pre-processing regular Expressions while(I < Plen) {if(i +1< Plen&&p[i +1] ==' * ') {Arr[alen] + = P[i]; Arr[alen] + = P[i +1]; i = i +2; }Else{Arr[alen] + = P[i]; i++; count_p_single++; } alen++; }//vector<int> vec_i; I all the current candidate positions on the string s //vector<int> Vec_j; J All current candidate locations on the string arri = j =0;//vec_i.push_back (i); //vec_j.push_back (j); intqueue_i[100000];//i All current candidate positions on the string s intqueue_j[100000];//j All current candidate locations on the string arr intTail_i;//Team tail, into the team operation intHead_i;//Head, team Operation intTail_j;intHead_j; tail_i = Tail_j = Head_i = Head_j =0; queue_i[tail_i++%100000] =0; Queue_j[tail_j++%100000] =0;//while (vec_i.size () > 0) { while(Head_i%100000! = tail_i%100000){//i = Vec_i.back (); //j = Vec_j.back (); //vec_i.pop_back (); //vec_j.pop_back ();i = queue_i[head_i++%100000]; j = queue_j[head_j++%100000];if(i = = Slen && J = = Alen)return true;if(i = = Slen && j<alen) {intFlag =0; for(intK = J; K < Alen; k++)if(arr[k].length () = =1) flag =1;if(!flag)return true; }if(I >= Slen | | J >= Alen | | slen-i < count_p_single)Continue;//Condition 1:a or. if(arr[j].length () = =1){if(arr[j][0] = = S[i] | | arr[j][0] =='. ') {i++; j + +;//vec_i.push_back (i); //vec_j.push_back (j);queue_i[tail_i++%100000] = i; Queue_j[tail_j++%100000] = J; count_p_single--; }Else{Continue; } }//Situation 2:a* Else if(arr[j].length () = =2&& arr[j][0] !='. '){//vec_i.push_back (i); //vec_j.push_back (j + 1);queue_i[tail_i++%100000] = i; Queue_j[tail_j++%100000] = j +1;if(arr[j][0]==s[i]) {CharGoal = S[i];intCounts =0, CountA =0; while(i < slen&&s[i] = = goal) {counts++; i++; } while(J < alen&&arr[j][0] = = goal) {if(arr[j].length () = =1) counta++; j + +; }//AAA 3 //a*aa* 1 3>=1 if(CountA > Counts)Continue; for(intx = CountA; x <= counts; X + +) {//vec_i.push_back (i-counts+x); //vec_j.push_back (j);queue_i[tail_i++%100000] = i-counts + x; Queue_j[tail_j++%100000] = J; } } }//Situation 3:.* Else if(arr[j].length () = =2&& arr[j][0] =='. '){if(Alen-1-J <=0)return true; j = j +1; for(intm = i; M <= Slen; m++) {//vec_i.push_back (m); //vec_j.push_back (j);queue_i[tail_i++%100000] = m; Queue_j[tail_j++%100000] = J; } } }return false; }};
Leetcode010:regular Expression Matching