lintcode 正则表达式匹配 ac代码

前端之家收集整理的这篇文章主要介绍了lintcode 正则表达式匹配 ac代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

http://www.lintcode.com/zh-cn/problem/regular-expression-matching/

  1. class Solution {
  2. public:
  3. /**
  4. * @param s: A string
  5. * @param p: A string includes "." and "*"
  6. * @return: A boolean
  7. */
  8. bool isMatch(const char *s,const char *p) {
  9. // write your code here
  10. const char*str,*ptr;
  11. bool start = false;
  12. int flag;
  13. int n=0,m =0 ;
  14. for (str = s; *str != '\0'; str++)n++;
  15. for (ptr = p; *ptr != '\0'; ptr++)m++;
  16. n--,m--;
  17. while (n>=0)
  18. {
  19. if (p[m] == s[n] || p[m] == '.')m--,n--;
  20. else if(p[m] == '*'){
  21. if (p[m - 1] != s[n]){//s和*前面的不一样
  22. if ( p[m - 1] == '.'){//如果前面是.
  23. if (m - 2 >= 0){//如果*前面至少还有2个字符
  24. start = true;
  25. flag = m;
  26. while (s[n] != p[m - 2]){//如果s有存在.*前面的字符
  27. n--;
  28. }
  29. }
  30. if (m == 1) return true;//如果最后剩下.*就return true;
  31. }
  32. }
  33. else{
  34. while (p[m - 1] == s[n]){
  35. n--;
  36. }
  37. }
  38. m -= 2;
  39. }
  40. else
  41. if (start){
  42. m = flag;
  43. }
  44. else return false;
  45.  
  46. }
  47. while (m>0&& p[m] == '*')
  48. m -= 2;
  49. return n==-1&&m==-1;
  50. }
  51. };

猜你在找的正则表达式相关文章