【字符串匹配】KMP(implement strStr()), 正则匹配(Wildcard Matching),2-dim 动规(regular expression)

前端之家收集整理的这篇文章主要介绍了【字符串匹配】KMP(implement strStr()), 正则匹配(Wildcard Matching),2-dim 动规(regular expression)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1 KMP

Implement strStr()

Total Accepted:15450 Total Submissions:71217 My Submissions

Implement strStr().

Returns a pointer to the first occurrence of needle in haystack,ornullif needle is not part of haystack.

  1. class Solution {
  2. public:
  3. char *strStr(char *haystack,char *needle) {
  4. int n=strlen(needle);
  5. if(n==0) return haystack;
  6. //kmp
  7. vector<int> nxt(n+1);///@error: nxt should be sized n+1,not n. to contain the nxt[n]
  8. nxt[0]=-1;nxt[1]=0;
  9. for(int i=0;i<n;i++){
  10. int j=nxt[i];
  11. while(j>=0&&needle[i]!=needle[j]) j=nxt[j]; //i matched j
  12. nxt[i+1]=j+1;/////@@@@@@@error: nxt[i+1]=j+1; mistake as needle[i+1]=j+1; an Array_Variable_Fault !!!!
  13. }
  14. int j=0;
  15. for(int i=0;haystack[i]!=0;i++,j++){
  16. while(j>=0&&haystack[i]!=needle[j]) j=nxt[j];//i matched j
  17. if(j==n-1) return haystack+i-j; //matche needle
  18. }
  19. return NULL;
  20. }
  21. };



正则匹配不同于一般匹配。回溯取决于*号出现的位置。

采用贪心法:p中'*'尽量匹配s中最少的字符,从上到下执行规则:

1.1如果p到达0,s未到达0: 则回溯匹配失败

1.2 如果p到达0,s到达0: 则成功

2.1如果p为'*':设置回溯点,p前进

3.1 如果p不为0或'*',而s到达0: 则匹配失败

3.2 如果p不为0或'*',s不为0: p=s或p='?'则s、p前进;否则回溯匹配失败



Wildcard Matching

Total Accepted:10189 Total Submissions:75093 My Submissions

Implement wildcard pattern matching with support for'?'and'*'.

  1. '?' Matches any single character.
  2. '*' Matches any sequence of characters (including the empty sequence).
  3.  
  4. The matching should cover the entire input string (not partial).
  5.  
  6. The function prototype should be:
  7. bool isMatch(const char *s,const char *p)
  8.  
  9. Some examples:
  10. isMatch("aa","a") false
  11. isMatch("aa","aa") true
  12. isMatch("aaa","aa") false
  13. isMatch("aa","*") true
  14. isMatch("aa","a*") true
  15. isMatch("ab","?*") true
  16. isMatch("aab","c*a*b") false
  1. class Solution {
  2. public:
  3. bool isMatch(const char *s,const char *p) {
  4. const char *pre_s,*pre_p;bool star=false;///@@error:pre_s pre_p must be const,or invalid conversion from 'const char*' to 'char*' [-fpermissive]
  5. while(true){
  6. if(*p==0){
  7. if(*s==0) return true; //*p=0 and *p matches *s
  8. if(!star) return false; //*p=0!=*s
  9. s=++pre_s;p=pre_p;
  10. }
  11. else if(*p!='*'){
  12. if(*p==*s||*p=='?'&&*s!=0) s++,p++;//*p!=0 and *p matches *s (then *s!=0)
  13. else {
  14. if(!star||*s==0) return false;///@warning:when *s=0,say s reaches the end,p will not match it even if Star=true
  15. s=++pre_s;p=pre_p;
  16. }
  17. }else{
  18. star=true;
  19. pre_s=s,pre_p=p+1;
  20. p=pre_p;
  21. }
  22. }
  23. }
  24. };

或者

  1. class Solution {
  2. public:
  3. bool isMatch(const char *s,const char *p) {
  4. // Start typing your C/C++ solution below
  5. // DO NOT write int main() function
  6. const char *pre_s = s,*pre_p = p;//保存上一次开始匹配的位置
  7. bool has_star = false;
  8.  
  9. while (*s) {
  10. if (*s != *p) {
  11. if (*p == '?') s++,p++;
  12. else if (*p == '*') {
  13. has_star = true;
  14. while (*p == '*') p++;
  15. if (*p == '\0') return true;
  16. pre_s = s,pre_p = p;//置上一个开始比较的位置
  17. } else if (has_star) {
  18. pre_s++;
  19. s = pre_s,p = pre_p;//恢复到上一次比较的下一个位置
  20. } else return false; //*s!=0 and *s!=*p and *p!='?' and !hasStar
  21. } else s++,p++;
  22. }
  23. //s has reach the end,so p's remainning part must match ''.
  24. while (*p == '*') p++;
  25. return *p == '\0';
  26. }
  27. };

或者:

  1. class Solution {
  2. public:
  3. bool isMatch(const char *s,const char *p) {
  4. bool tag = false;
  5. const char * pre_s,*pre_p;
  6. while(*s){
  7. if(*s==*p || *p=='?') s++,p++;
  8. else if(*p=='*'){
  9. tag = true;
  10. pre_s = s,pre_p = ++p;
  11. }
  12. else{
  13. if(tag){
  14. s = ++ls,p = pre_p;
  15. }
  16. else return false;
  17. }
  18. }
  19. while(*p == '*') p++;
  20. return *p==0;
  21. }
  22. };



Regular Expression Matching

Total Accepted:13590 Total Submissions:68280 My Submissions

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

  1. '.' Matches any single character.
  2. '*' Matches zero or more of the preceding element.
  3.  
  4. The matching should cover the entire input string (not partial).
  5.  
  6. The function prototype should be:
  7. bool isMatch(const char *s,"a*") true
  8. isMatch("aa",".*") true
  9. isMatch("ab",".*") true
  10. isMatch("aab","c*a*b") true

一个很典型的二维动规的实例
for(char in Pattern)
for(char in String)

  1. class Solution {
  2. public:
  3. inline bool match(char a,char b){
  4. //assert(a!='*');
  5. if(a=='.') return true;
  6. return a==b;
  7. }
  8. bool isMatch(const char *s,const char *p) {
  9. int n=strlen(p),m=strlen(s);
  10. vector<vector<bool> > a(n+1,vector<bool>(m+1,false));
  11.  
  12. a[0][0]=true;
  13. /*
  14. for(int i=2;i<=n;i+=2){
  15. if(p[i-1]!='*') break;
  16. a[i][0]=true;
  17. }
  18. for(int j=1;j<=m;j++)
  19. a[0][j]=false;
  20. */
  21.  
  22. for(int i=1;i<=n;i++){
  23. if(i<n&&p[i]=='*') continue;//i++,go to '*'
  24. if(p[i-1]=='*'){
  25. assert(i>1&&p[i-2]!='*');
  26. char c=p[i-2];
  27. bool lc=false;
  28. for(int j=0;j<=m;j++){//1-dim dp
  29. if(j>0&&lc) lc=match(c,s[j-1]);///@error:s[j-1] fault as a[j-1]///@error:if(j>0&&lc&&match(c,s[j-1])) lc=true; here,when lc=true,lc should be false if match(c,s[j-1])=false
  30. if(a[i-2][j]) lc=true;
  31. a[i][j]=lc;
  32. }
  33. }
  34. else
  35. for(int j=1;j<=m;j++){
  36. if((p[i-1]==s[j-1]||p[i-1]=='.')&& a[i-1][j-1]) a[i][j]=true;
  37. }
  38. }
  39. return a[n][m];
  40. }
  41. };

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