剑指offer面试题53

    xiaoxiao2023-11-21  5

    一不小心就看了答案,就拿来当作代码编写训练了

    #include<iostream> #include<string> #include<queue> using namespace std; /* 面试题53:正则表达式匹配 给一个字符串,和一个正则表达式,求他们是否匹配 ‘.’可以匹配任何字符串 ‘*’前面的字符可以出现任意次包括0次 */ bool matchCore(char *str,char *pat) { bool bool1,bool2; if(str==NULL||pat==NULL) //鲁棒性检测 return false; if(*str!='\0'&&*pat!='\0') { if(*(pat+1)=='*') { bool1=matchCore(str,pat+2); if(*str==*pat||(*pat=='.')) //只有*前面的字符相等时才能选择不跳过 bool2=matchCore(str+1,pat); else bool2=false; return bool1||bool2;//不跳过 } else if(*str==*pat||(*pat=='.')) { return matchCore(str+1,pat+1); } } if(*str=='\0'&&*pat=='\0') return true; else return false; } int main() { int i; char a[100],b[100]; while(1) { printf("输入字符串:"); scanf("%s",a); printf("输入匹配串:"); scanf("%s",b); if(matchCore(a,b)==true) printf("匹配!\n"); else printf("不匹配\n"); } }

    转载请注明原文地址: https://ju.6miu.com/read-1284170.html
    最新回复(0)