c - 如何检查 2 个字符串是否与包含 '*' 的字符串匹配,该字符串可以替换所有字符或字符串?

标签 c string-matching

我正在寻找提示来帮助我找到解决方案,我尝试了许多算法,但似乎没有一个算法适用于所有情况。我知道有一个递归解决方案,只需几行即可构建一个函数,但我想不出它。

以下是说明:

The purpose of this function is to find out whether two strings match.

  • s1 and s2 are considered to match when s1 and s2 are identical.

  • If s2 contains a star (’*’), we can replace this star by any characters string (even empty) to make s1 and s2 identical.

  • s2 may hold as many stars as you’d like.

  • For example, "main.c" and "*.c" match because it is possible to replace ’*’ by the string "main" to render those two strings identical.

  • Here’s how it should be prototyped : int match(char *s1, char *s2);

  • It must return 1 if s1 and s2 match, or 0 if they don’t.

我的函数将使用 C 语言。

困难的情况是:

s1 = "abcde" & s2 = "a*e*d"
s1 = "abcdecde" & s2 = "a*e"

最佳答案

这是您真正赢得 2001 年 IOCCC 的 glob 匹配器:

main(int c,char**v){return!m(v[1],v[2]);}
m(char*s,char*t){return*t-42?*s?63==*t|*s==*t&&m(s+1,t+1):!*t:m(s,t+1)||*s&&m(s+1,t);}

除了*之外,它还可以理解?恰好一个字符。剥离 ? 支持应该是一个有趣的练习:-) 阅读 hints 可能会有所帮助。 .

关于c - 如何检查 2 个字符串是否与包含 '*' 的字符串匹配,该字符串可以替换所有字符或字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49478437/

相关文章:

java - 匹配 arraylist 中的字符串,其中字符串是 arraylist 串联的结果

java - 正则表达式匹配不完整的表达式

java - 如何在Hadoop中实现字符串匹配算法?

python - C相当于Python的struct.pack?

c - 将项目推送到链表 (C)

python /赛通 : Using SciPy with Cython

R agrep() 函数行为

javascript - 如何使用 JavaScript 替换文章中的随机匹配文本?

c - C 循环中递增整数指针

c - X 秒后终止程序 Linux C