Ladies_Man

Word made of prefixes

Dec 17th, 2013
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int *pi;
  6.  
  7. void match (int ind, char *str)
  8. {
  9.     if (ind >= strlen(str)) {
  10.         printf("yes");
  11.         //return;
  12.     }
  13.     else {
  14.         printf("no");
  15.         //return;
  16.     }
  17. }
  18.  
  19. void prefix (char *str, int ind, int k)
  20. {
  21.     int i, t = 0;
  22.     pi[0] = 0;
  23.  
  24.     for (i = ind; i < strlen(str) + 1; i++) {
  25.         //t = pi[t - 1];
  26.         while ((t > 0) && (str[t] != str[i]))
  27.             t = pi[t - 1];
  28.         if (str[t] == str[i])
  29.                 t++;
  30.         pi[i] = t;
  31.         if ((i >= k) && (t == 0)) break;
  32.     }
  33.     match(i, str);
  34. }
  35.  
  36. void pword (char *s, char *t, int c)
  37. {
  38.     int i = 1;
  39.     pi = (int*)malloc((strlen(s)+strlen(t)+1) * sizeof(int));
  40.     //int n = c;
  41.  
  42.     prefix(s, i, c);
  43.  
  44.     /*for (i = n; i < strlen(s); i++) {
  45.         if (pi[i] == 0) {
  46.             printf("no");
  47.             return;
  48.         }
  49.         else {
  50.             printf("yes");
  51.             return;
  52.         }
  53.     }*/
  54.  
  55.  
  56.     //return;
  57. }
  58.  
  59. int main(int argc, char **argv)
  60. {
  61.     int i, ls = 0;
  62.     char *a = argv[1];
  63.     int c = strlen(a);
  64.     //scanf("%s", a);
  65.     char *b = argv[2];
  66.     //scanf("%s", b);
  67.         //pword(argv[1], argv[2]);
  68.  
  69.         int last_symb_a = strlen(a), last_symb_b = strlen(b);
  70.     for (i = 0; i < strlen(b); i++) {
  71.                 a[last_symb_a + i] = b[i];
  72.                 //ls++;
  73.     }
  74.     a[last_symb_a + last_symb_b + 1] = 0;
  75.  
  76.     pword(a, b, c);
  77.  
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment