bool recursion_util(string& text, string& pattern, int pos1, int pos2)
{
if (pos2 == pattern.size())
return pos1 == text.size();
if (pattern.at(pos2) != '*') {
if (pos1 < text.size() && (text.at(pos1) == pattern.at(pos2) || pattern.at(pos2) == '?'))
return recursion_util(text, pattern, pos1+1, pos2+1);
else
return false;
}
else {
if ((pos2+1) < text.size() && pattern.at(pos2+1) == '*') {
pos2++;
}
while (pos1 < text.size())
{
if (recursion_util(text, pattern, pos1, pos2+1))
return true;
pos1++;
}
return false;
}
}
bool recursion_wild_card(string text, string pattern) {
return recursion_util(text, pattern, 0, 0);
}
bool recursion_regular_util(string& text, string& pattern, int pos1, int pos2)
{
if (pos2 == pattern.size())
return pos1 == text.size();
if (pattern.at(pos2) != '*') {
if (pos1 < text.size() && (text.at(pos1) == pattern.at(pos2) || pattern.at(pos2) == '.'))
return recursion_regular_util(text, pattern, pos1+1, pos2+1);
return false;
}
else {
while (pos2 < pattern.size()-1 && pattern.at(pos2+1) == '*')
pos2++;
pos1--;
while (pos1 < text.size())
{
if (recursion_regular_util(text, pattern, pos1, pos2+1))
return text.at(pos1) == pattern.at(pos2-1) || pattern.at(pos2-1) == '.';
pos1++;
}
return false;
}
}
bool recursion_regular(string text, string pattern)
{
return recursion_regular_util(text, pattern, 0, 0);
}
通配符、正则表达式的回溯解法
最新推荐文章于 2022-06-09 21:05:29 发布