循环子问题,可以用动规或者递归,递归写起来好理解一点:
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
string s1;
string s2;
bool if_Match(int x,int y){
if(x==s1.length()&&y==s2.length())return true;
else if(x>s1.length()||y>s2.length())return false;
if(s1[x]==s2[y]) return if_Match(x+1,y+1);//递归
else if(s1[x]=='?'){
if(!(isdigit(s2[y])||isalpha(s2[y])))return false;//如果s2中存在不能匹配的 字符
return if_Match(x+1, y+1);//否则递归
}
else if(s1[x]=='*'){
while(s1[x+1]=='*')x++;//看下一个是不是继续*,是就继续看下一个
return if_Match(x,y+1)||if_Match(x+1,y)||if_Match(x+1,y+1);//不是,就匹配 *,三种情况,*匹配 null,*匹配一个,*匹配多个
}
return false;
}
int main(){
while(cin>>s1>>s2){
for(int i=0;i<s1.length();i++)
s1[i]=tolower(s1[i]);
for(int j=0;j<s2.length();j++)
s2[j]=tolower(s2[j]);
if(if_Match(0,0))
cout<<"true"<<endl;
else
cout<<"false"<<endl;
}
return 0;
}