http://www.lintcode.com/zh-cn/problem/regular-expression-matching/
class Solution { public: /** * @param s: A string * @param p: A string includes "." and "*" * @return: A boolean */ bool isMatch(const char *s,const char *p) { // write your code here const char*str,*ptr; bool start = false; int flag; int n=0,m =0 ; for (str = s; *str != '\0'; str++)n++; for (ptr = p; *ptr != '\0'; ptr++)m++; n--,m--; while (n>=0) { if (p[m] == s[n] || p[m] == '.')m--,n--; else if(p[m] == '*'){ if (p[m - 1] != s[n]){//s和*前面的不一样 if ( p[m - 1] == '.'){//如果前面是. if (m - 2 >= 0){//如果*前面至少还有2个字符 start = true; flag = m; while (s[n] != p[m - 2]){//如果s有存在.*前面的字符 n--; } } if (m == 1) return true;//如果最后剩下.*就return true; } } else{ while (p[m - 1] == s[n]){ n--; } } m -= 2; } else if (start){ m = flag; } else return false; } while (m>0&& p[m] == '*') m -= 2; return n==-1&&m==-1; } };