Guest User

Untitled

a guest
Mar 15th, 2016
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.04 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5.  
  6. static unsigned 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.  
  17.     if (l > strlen(str))
  18.         return false;
  19.  
  20.     if ((*list[pos] != *str) || strncmp(list[pos], str, l))
  21.         return _is_concat(list, ++pos, sz, str);
  22.  
  23.     if (_is_concat(list, 0, sz, str + l))
  24.         return true;
  25.  
  26.     return _is_concat(list, ++pos, sz, str);
  27. }
  28.  
  29. static bool is_concat(const char *const *list, size_t sz, const char *str)
  30. {
  31.     if (!str || !strlen(str))
  32.         return false;
  33.     return _is_concat(list, 0, sz, str);
  34. }
  35.  
  36. static int compare(const void *a, const void *b)
  37. {
  38.     const char *const *aa = a;
  39.     const char *const *bb = b;
  40.  
  41.     if (strlen(*aa) < strlen(*bb))
  42.         return -1;
  43.  
  44.     return 1;
  45. }
  46.  
  47.  
  48. int main(int argc, char **argv)
  49. {
  50.     static const char *test_list[] = {
  51.             "a",
  52.             "ab",
  53.             "abc",
  54.             "abcd",
  55.             "abcde",
  56.             "abcdef",
  57.             "abcdefg",
  58.             "abcdefgh",
  59.             "abcdefghj",
  60.             "abcdefghji",
  61.             "abcdefghjij",
  62.             "abcdefghjijk",
  63.             "abcdefghjijkl",
  64.             "abcdefghjijklm",
  65.             "abcdefghjijklmn",
  66.             "abcdefghjijklmno",
  67.             "abcdefghjijklmnop",
  68.             "abcdefghjijklmnopq",
  69.             "abcdefghjijklmnopqr",
  70.             "abcdefghjijklmnopqrs",
  71.             "abcdefghjijklmnopqrst",
  72.             "abcdefghjijklmnopqrstu",
  73.             "b",
  74.             "bc",
  75.             "bcd",
  76.             "bcde",
  77.             "bcdef",
  78.             "bcdefg",
  79.             "bcdefgh",
  80.             "bcdefghj",
  81.             "bcdefghji",
  82.             "bcdefghjij",
  83.             "bcdefghjijk",
  84.             "bcdefghjijkl",
  85.             "bcdefghjijklm",
  86.             "bcdefghjijklmn",
  87.             "bcdefghjijklmno",
  88.             "bcdefghjijklmnop",
  89.             "bcdefghjijklmnopq",
  90.             "bcdefghjijklmnopqr",
  91.             "bcdefghjijklmnopqrs",
  92.             "bcdefghjijklmnopqrst",
  93.             "bcdefghjijklmnopqrstu",
  94.             "c",
  95.             "cd",
  96.             "cde",
  97.             "cdef",
  98.             "cdefg",
  99.             "cdefgh",
  100.             "cdefghj",
  101.             "cdefghji",
  102.             "cdefghjij",
  103.             "cdefghjijk",
  104.             "cdefghjijkl",
  105.             "cdefghjijklm",
  106.             "cdefghjijklmn",
  107.             "cdefghjijklmno",
  108.             "cdefghjijklmnop",
  109.             "cdefghjijklmnopq",
  110.             "cdefghjijklmnopqr",
  111.             "cdefghjijklmnopqrs",
  112.             "cdefghjijklmnopqrst",
  113.             "cdefghjijklmnopqrstu",
  114.             "d",
  115.             "de",
  116.             "def",
  117.             "defg",
  118.             "defgh",
  119.             "defghj",
  120.             "defghji",
  121.             "defghjij",
  122.             "defghjijk",
  123.             "defghjijkl",
  124.             "defghjijklm",
  125.             "defghjijklmn",
  126.             "defghjijklmno",
  127.             "defghjijklmnop",
  128.             "defghjijklmnopq",
  129.             "defghjijklmnopqr",
  130.             "defghjijklmnopqrs",
  131.             "defghjijklmnopqrst",
  132.             "defghjijklmnopqrstu",
  133.             "e",
  134.             "ef",
  135.             "efg",
  136.             "efgh",
  137.             "efghj",
  138.             "efghji",
  139.             "efghjij",
  140.             "efghjijk",
  141.             "efghjijkl",
  142.             "efghjijklm",
  143.             "efghjijklmn",
  144.             "efghjijklmno",
  145.             "efghjijklmnop",
  146.             "efghjijklmnopq",
  147.             "efghjijklmnopqr",
  148.             "efghjijklmnopqrs",
  149.             "efghjijklmnopqrst",
  150.             "efghjijklmnopqrstu",
  151.             "f",
  152.             "fg",
  153.             "fgh",
  154.             "fghj",
  155.             "fghji",
  156.             "fghjij",
  157.             "fghjijk",
  158.             "fghjijkl",
  159.             "fghjijklm",
  160.             "fghjijklmn",
  161.             "fghjijklmno",
  162.             "fghjijklmnop",
  163.             "fghjijklmnopq",
  164.             "fghjijklmnopqr",
  165.             "fghjijklmnopqrs",
  166.             "fghjijklmnopqrst",
  167.             "fghjijklmnopqrstu",
  168.             "g",
  169.             "gh",
  170.             "ghj",
  171.             "ghji",
  172.             "ghjij",
  173.             "ghjijk",
  174.             "ghjijkl",
  175.             "ghjijklm",
  176.             "ghjijklmn",
  177.             "ghjijklmno",
  178.             "ghjijklmnop",
  179.             "ghjijklmnopq",
  180.             "ghjijklmnopqr",
  181.             "ghjijklmnopqrs",
  182.             "ghjijklmnopqrst",
  183.             "ghjijklmnopqrstu",
  184.             "h",
  185.             "hj",
  186.             "hji",
  187.             "hjij",
  188.             "hjijk",
  189.             "hjijkl",
  190.             "hjijklm",
  191.             "hjijklmn",
  192.             "hjijklmno",
  193.             "hjijklmnop",
  194.             "hjijklmnopq",
  195.             "hjijklmnopqr",
  196.             "hjijklmnopqrs",
  197.             "hjijklmnopqrst",
  198.             "hjijklmnopqrstu",
  199.             "j",
  200.             "ji",
  201.             "jij",
  202.             "jijk",
  203.             "jijkl",
  204.             "jijklm",
  205.             "jijklmn",
  206.             "jijklmno",
  207.             "jijklmnop",
  208.             "jijklmnopq",
  209.             "jijklmnopqr",
  210.             "jijklmnopqrs",
  211.             "jijklmnopqrst",
  212.             "jijklmnopqrstu",
  213.             "i",
  214.             "ij",
  215.             "ijk",
  216.             "ijkl",
  217.             "ijklm",
  218.             "ijklmn",
  219.             "ijklmno",
  220.             "ijklmnop",
  221.             "ijklmnopq",
  222.             "ijklmnopqr",
  223.             "ijklmnopqrs",
  224.             "ijklmnopqrst",
  225.             "ijklmnopqrstu",
  226.             "j",
  227.             "jk",
  228.             "jkl",
  229.             "jklm",
  230.             "jklmn",
  231.             "jklmno",
  232.             "jklmnop",
  233.             "jklmnopq",
  234.             "jklmnopqr",
  235.             "jklmnopqrs",
  236.             "jklmnopqrst",
  237.             "jklmnopqrstu",
  238.             "k",
  239.             "kl",
  240.             "klm",
  241.             "klmn",
  242.             "klmno",
  243.             "klmnop",
  244.             "klmnopq",
  245.             "klmnopqr",
  246.             "klmnopqrs",
  247.             "klmnopqrst",
  248.             "klmnopqrstu",
  249.             "l",
  250.             "lm",
  251.             "lmn",
  252.             "lmno",
  253.             "lmnop",
  254.             "lmnopq",
  255.             "lmnopqr",
  256.             "lmnopqrs",
  257.             "lmnopqrst",
  258.             "lmnopqrstu",
  259.             "m",
  260.             "mn",
  261.             "mno",
  262.             "mnop",
  263.             "mnopq",
  264.             "mnopqr",
  265.             "mnopqrs",
  266.             "mnopqrst",
  267.             "mnopqrstu",
  268.             "n",
  269.             "no",
  270.             "nop",
  271.             "nopq",
  272.             "nopqr",
  273.             "nopqrs",
  274.             "nopqrst",
  275.             "nopqrstu",
  276.             "o",
  277.             "op",
  278.             "opq",
  279.             "opqr",
  280.             "opqrs",
  281.             "opqrst",
  282.             "opqrstu",
  283.             "p",
  284.             "pq",
  285.             "pqr",
  286.             "pqrs",
  287.             "pqrst",
  288.             "pqrstu",
  289.             "q",
  290.             "qr",
  291.             "qrs",
  292.             "qrst",
  293.             "qrstu",
  294.             "r",
  295.             "rs",
  296.             "rst",
  297.             "rstu",
  298.             "s",
  299.             "st",
  300.             "stu",
  301.             "t",
  302.             "tu",
  303.             "u",
  304.     };
  305.     size_t len = sizeof(test_list) / sizeof(test_list[0]);
  306.     qsort(test_list, len, sizeof(char *), compare);
  307.  
  308.     call_cnt = 0;
  309.     printf("abcdefghjijklmnopqrstuz: %d\n", is_concat(test_list, len, "abcdefghjijklmnopqrstuz"));
  310.     printf("call_cnt: %u\n", call_cnt);
  311.  
  312.     return 0;
  313. }
Advertisement
Add Comment
Please, Sign In to add comment