华为oj 字符串通配符

来源:互联网 发布:dd打车软件 编辑:程序博客网 时间:2024/05/21 19:46
#include<iostream>#define N 1000 using namespace std;//整个算法的难点在于*找到第一个匹配的位置,采用递归的方式找到第一个后项完全匹配的位置,如果没有的找到,那么当前被匹配的指针后移直到遇到\0 bool isLegal(char c){if(isalpha(c)||isdigit(c))   return true;else   return false;}bool isMatch(char *a,char *b){if(*a == '\0')   return *b == '\0';else if(*a == '?'){   if(isLegal(*b))       return isMatch(a+1,b+1);   else       return false;}else if (*a == '*'){//首先找到*后面的第一个非*字符while(*a == '*')     ++a;//然后找到要匹配的字符串中第一个符合当前非*字符的位置while(*b != '\0'){ if(isMatch(a,b)) //表示当前的字符和后面的全部都匹配,否则可能是当前的不匹配,也可能是后面的不匹配,就需要往后移找匹配位置                  return true; else {    if(isLegal(*b))    ++b;   else         return false; }} if(*b=='\0'&& *a == *b)      return true;else     return false;}else{ if(*a == *b)    return isMatch(a+1,b+1); else    return false;} }int main(){char a[N],b[N];gets(a);gets(b);if(isMatch(a,b))  cout<<"true"<<endl; else  cout<<"false"<<endl;return 0; } 

0 0
原创粉丝点击