是这样的,我想写一个通配符匹配的C语言函数供我的程序使用,考虑到这玩意这么成熟了应该直接拿别人的就行了,没必要浪费时间,于是我上网查了一下,发现有两种实现,都拿来用测了一下,发现都可以用,于是进一步进行测试效率,以便选择最好的。
通配符匹配函数传入的参数有:字符串,模板。例如:“c:\\124.txt”和"124.*"。
目前两种不同的方法:
1.方法一:遍历字符串和模板的字符,一个一个地分析处理;
2.方法二:创建初始化一个查找表,然后在查找表中遍历处理得到结果;
咋一看,似乎有查找表的方式看起来更厉害似的,既然用上了空间,那么就应该时间吧。
但是经过测试发现,同样的数据处理,方法二耗时竟然是方法一的三十倍左右,实在是大跌眼镜!
c语言解决办法,永远是那么直接和高效。
下面分别贴出代码:
方法一:
bool wildcardMatch(char *subject, char *pattern) {
for (; *pattern; pattern++) {
switch (*pattern) {
case '*':
if (*(pattern + 1) == '\0')
return true;
for (char *s = subject; *s; s++) {
if (wildcardMatch(s, pattern + 1))
return true;
}
return false;
case '?':
if (*(subject++) == '\0')
return false;
break;
default:
if (*subject == '\0' || *pattern != *(subject++))
return false;
}
}
return *subject == '\0';
}
方法二:
bool isMatch(const char *word, const char *pattern, int n, int m, vector<vector<bool>>& lookup) {
// If both the input string and pattern reach their end,
// return true
if (m < 0 && n < 0) {
return true;
}
// If only the pattern reaches its end, return false
else if (m < 0) {
return false;
}
// If only the input string reaches its end, return true
// if the remaining characters in the pattern are all '*'
else if (n < 0) {
for (int i = 0; i <= m; i++) {
if (pattern[i] != '*') {
return false;
}
}
return true;
}
// If the subproblem is encountered for the first time
if (!lookup[m][n]) {
if (pattern[m] == '*') {
// 1. '*' matches with current characters in the input string.
// Here, we will move to the next character in the string.
// 2. Ignore '*' and move to the next character in the pattern
lookup[m][n] = isMatch(word, pattern, n - 1, m, lookup) ||
isMatch(word, pattern, n, m - 1, lookup);
} else {
// If the current character is not a wildcard character, it
// should match the current character in the input string
if (pattern[m] != '?' && pattern[m] != word[n]) {
lookup[m][n] = 0;
}
// check if pattern[0…m-1] matches word[0…n-1]
else {
lookup[m][n] = isMatch(word, pattern, n - 1, m - 1, lookup);
}
}
}
return lookup[m][n];
}