python带通配符的字符串匹配_含有通配符的字符串匹配

字符串匹配问题,给定两个字符串。求字符串2。在字符串1中的最先匹配结果。字符串2中能够存在'*'符号,且该符号能够代表随意字符,即字符串2中存在通配符。

e.g. 输入:abcdefghabef, a*f 输出:abcdef

#include

#include

using namespace std;

bool Match(const string &s1,const string &s2,string &result)

{

int i=0;

if(s2.empty())//已经到了s2的尾部。说明都匹配了(s2和s1的推断顺序不能改)

return true;

if(s1.empty())//s1已经到了尾部。而s2还没到尾部,说明没有全然匹配

{

result="";

return false;

}

if(s1[i]==s2[i])//假设相等,则匹配了一个元素。接着依次匹配下一个元素

{

result.push_back(s1[i]);

Match(s1.substr(i+1),s2.substr(i+1),result);

}

else if(s2[i]=='*')//假设遇到*号。则跳过*号,匹配s2的其它元素

{

Match(s1,s2.substr(i+1),result);

}

else//假设s1和s2的第一个元素不相等,则匹配s1的下一个元素

{

result.push_back(s1[i]);

Match(s1.substr(i+1),s2,result);

}

}

int main()

{

string s1="abcdefghabef";

string s2="a*f";

string result;

Match(s1,s2,result);

cout<

return 0;

}

參考

http://www.tuicool.com/articles/YZFJBb

http://wenku.it168.com/d_001232271.shtml

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值