fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int MatchesPattern(const char *s, const char *pattern)
  6. {
  7. size_t prefixSize = strcspn(pattern, "*?");
  8. if (memcmp(s, pattern, prefixSize) != 0)
  9. return 0;
  10.  
  11. pattern += prefixSize;
  12. s += prefixSize;
  13.  
  14. if (*pattern == '?')
  15. {
  16. return *s && MatchesPattern(s + 1, pattern + 1);
  17. }
  18. if (*pattern == '*')
  19. {
  20. while (*pattern == '*')
  21. ++pattern;
  22. if (!*pattern)
  23. return 1;
  24. while (*s)
  25. {
  26. if (MatchesPattern(s, pattern))
  27. return 1;
  28. ++s;
  29. }
  30. return !*pattern;
  31. }
  32. return !*s;
  33. }
  34.  
  35. int main(void) {
  36. char s[0x100];
  37. char pattern[0x100];
  38. char resultLine[0x100];
  39. int failCount = 0;
  40. while (gets(pattern) && gets(s) && gets(resultLine))
  41. {
  42. int expected = atoi(resultLine);
  43. int result = MatchesPattern(s, pattern);
  44. if (expected != result)
  45. {
  46. ++failCount;
  47. printf("%s\n%s\n%s\n-------\n", pattern, s, result?"Match":"do not match");
  48. }
  49. }
  50.  
  51. return failCount?1:0;
  52. }
  53.  
Success #stdin #stdout 0s 2168KB
stdin

1

asd
0
sameNoPattern
sameNoPattern
1
diffrent
diffrentNo pattern
0
*
wild  match
1
w*
wild  match prefix ok
1
x*
wild  match prefix fail
0
*Ok
wild  match suffix Ok
1
*ls
wild  match suffix fail
0
?ingle match prefix OK
single match prefix OK
1
?ingle match prefix fail
single match prefix fail!
0
single match suffix O?
single match suffix Ok
1
single match suffix fa?
single match suffix fail
0
single match suffix length fail?
single match suffix length fail
0
single matc? inside ok
single match inside ok
1
wild match *ok
wild match in the middle ok
1
wild match * not ok
wild match in the middle fail
0
wild match -* fail
wild match in the middle fail
0
to wild * in the *Ok
to wild matches in the middle Ok
1
stdout
Standard output is empty