Leetcode--regular Expression Matching

Source: Internet
Author: User

Implement regular expression matching with support for ‘.‘ and ‘*‘ .

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
This paper uses the method of regular expression matching, simulates the string p with the non-deterministic state automata, and then matches the string, and the time complexity of the algorithm is O (N);

Problem Solving Code:

Class Graph{private:struct Node{int wei;node* next;}; node* g;int v;bool* marked;void dfs (const int s) {Marked[s] = true;vector<int> temp = adj (s); for (int i=0; I&LT;TEMP.S Ize (); i++) {if (!marked[temp[i])) DFS (Temp[i]);} return;} Public:graph (const int v) {this->v = v;marked = new Bool[v];g = new node[v];for (int i=0; i<v; i++) {Marked[i] = false; G[i].wei = 0;g[i].next = nullptr;}} ~graph () {delete[] marked;for (int i=0; i<v; i++) {node* Temp1 = G[i].next;while (temp1!=nullptr) {node* temp2 = Temp1; Temp1 = Temp1->next;delete Temp2;}} Delete[] g;} BOOL is_marked (const int v) {return marked[v];} void restore_marked () {for (int i=0; i<v; i++) marked[i] = false;} void Add_edge (const int V, const int W) {node* temp = new Node;temp->wei = W;temp->next = G[v].next;g[v].next = temp; }void Dfs (vector<int>& start) {int n = start.size (); for (int i=0; i<n; i++) if (!marked[start[i]]) DFS (start[i ]);} vector<int>& adj (const int v) {vector<int>* res = new Vector<int>;node* temp = g[v].next;while (temp!=nullptr) {res->push_back (temp->wei); temp = Temp->next;} return *res;}; Class Solution {Public:bool isMatch (const char *s, const char *p) {if (Has_star (p)) return match (s,p); int I=0;while (s[i]!= ' \ && P[i]! = ' + ') {if (s[i]== '. ' | | p[i]== '. ') {i++;continue;} if (s[i]! = P[i]) return false;i++;} if (s[i]!= ' | | | p[i]!= ') return False;return true;} BOOL Match (const char* s, const char* p) {String temp_s = char_to_string (s); string temp_p = Char_to_string (p); int n = temp_ P.length (); graph G (n+1); for (int i=0; i<n; i++) {if (p[i] = = ' * ') {G.add_edge (i,i-1); G.add_edge (i-1,i); G.add_edge (i,i +1);}} vector<int> pc;vector<int> marked;marked.push_back (0); G.dfs (marked); for (int i=0; i<n+1; i++) {if (g.is _marked (i)) pc.push_back (i); for (int i=0; I<temp_s.length (), i++) {marked.clear (); for (int v=0; v<pc.size (); v++) {if (pc[v]<n) {if (P[pc[v]] = = S[i] | | P[PC[V]] = = '. ') Marked.push_back (pc[v]+1);}} Pc.clear (); g.restore_marked (); G.dfs (marked); for (int i=0; i<n+1; i++) {if (g.is_marked (i)) pc.push_back (i);}} for (int i=0; i<pc.size (); i++) {if (pc[i] = = N) return true; return false;} BOOL Has_star (const char* s) {int i=0;while (s[i]! = ' + ') {if (s[i] = = ' * ') return true;i++;} return false;} String char_to_string (const char* s) {String temp = ""; int i=0;while (s[i]! = ' + ') {temp = temp + s[i];i++;} return temp;};



Leetcode--regular Expression Matching

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.