Guest User

Untitled

a guest
Mar 15th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5.  
  6. static int call_cnt;
  7.  
  8. static bool _is_concat(const char *const *list, size_t pos, size_t sz, const char *str)
  9. {
  10.     call_cnt++;
  11.     if (pos == sz)
  12.         return false;
  13.     if (!strlen(str))
  14.         return true;
  15.     const size_t l = strlen(list[pos]);
  16.     if (strncmp(list[pos], str, l)) {
  17.         return _is_concat(list, ++pos, sz, str);
  18.     } else {
  19.         if (_is_concat(list, 0, sz, str + l))
  20.             return true;
  21.  
  22.         return _is_concat(list, ++pos, sz, str);
  23.     }
  24. }
  25.  
  26. static bool is_concat(const char *const *list, size_t sz, const char *str)
  27. {
  28.     if (!str || !strlen(str))
  29.         return false;
  30.     return _is_concat(list, 0, sz, str);
  31. }
  32.  
  33. int main(int argc, char **argv)
  34. {
  35.     static const char *test_list[] = {
  36.         "a", "b", "c", "d", "e", "f", "g", "h", "i"
  37.     };
  38.     size_t len = sizeof(test_list) / sizeof(test_list[0]);
  39.  
  40.     call_cnt = 0;
  41.     printf("abcdefghiabcXdefghi: %d\n", is_concat(test_list, len, "abcdefghiabcXdefghi"));
  42.     printf("call_cnt: %d\n", call_cnt);
  43.  
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment