Advertisement
chunkyguy

gcj'11 - magicka

May 7th, 2011
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.25 KB | None | 0 0
  1. /*
  2.     Magicka
  3.     sid@whackylabs.com
  4.  */
  5.  
  6. #include<stdio.h>
  7. #include<string.h>
  8. #include<stdlib.h>
  9.  
  10. #define C_MAX 40
  11. #define D_MAX 30
  12. #define N_MAX 105
  13. #define DEBUG 0
  14.  
  15. int is_reacting(int c_strs_len, char **c_strs, char x, char y)
  16. {
  17.     int i;
  18.     char *c_str;
  19.     int react = -1;
  20.     for(i = 0 ; i < c_strs_len && react < 0; i++)
  21.     {
  22.         c_str = c_strs[i];
  23.         if(x == '.' || y == '.')
  24.             continue;
  25.        
  26.         if((c_str[0] == x && c_str[1] == y) || (c_str[0] == y && c_str[1] == x))
  27.             react = i;
  28.     }
  29.     return react;
  30. }
  31.  
  32. void process(int case_no, char **constr, int c_no, char **dstr, int d_no, char *case_str)
  33. {
  34.     char *list;
  35.     int i,j, k, match;
  36.     int print_comma = 0;
  37.    
  38.     if(DEBUG)
  39.     {
  40.         printf("\nprocessing case #: %d\n",case_no+1);
  41.         printf("constructors:\n");
  42.         for(i = 0; i < c_no; i++)
  43.             printf("%s\n",constr[i]);
  44.         printf("destructors:\n");
  45.         for(i = 0; i < d_no; i++)
  46.             printf("%s\n",dstr[i]);
  47.         printf("case-str: %s\n",case_str);
  48.         printf("\n");
  49.     }
  50.    
  51.    
  52.     list = (char *)malloc(sizeof(char) * strlen(case_str));
  53.     strcpy(list,case_str);
  54.    
  55.     for(i = 1; i < strlen(list); i++)
  56.     {
  57.         //construction
  58.         if((match = is_reacting(c_no, constr,list[i],list[i-1])) >= 0)
  59.         {
  60.             list[i] = constr[match][2];
  61.             list[i-1] = '.';
  62.         }
  63.         else
  64.         {
  65.             //destruction
  66.             for(j = i-1; j >= 0; j--)
  67.             {
  68.                 if((match =  is_reacting(d_no, dstr,list[i],list[j])) >= 0)
  69.                 {
  70.                     for(k = j; k <= i; k++)
  71.                         list[k] = '.';
  72.                 }
  73.             }
  74.         }
  75.         if(DEBUG) printf("-- HERE :%d --\n",i);
  76.     }
  77.    
  78.     //print
  79.     printf("Case #%d: [",case_no+1);
  80.     for(i = 0; i < strlen(list);)
  81.     {
  82.         if(list[i] == '.')
  83.         {
  84.             i++;
  85.             continue;
  86.         }
  87.        
  88.         if(print_comma)
  89.             printf(", ");
  90.         else
  91.             print_comma = 1;
  92.         printf("%c",list[i++]);
  93.     }
  94.     printf("]\n");
  95.     free(list);
  96. }
  97.  
  98. int main(void)
  99. {
  100.     int t_cases, c_no, d_no;
  101.     char **constr;
  102.     char **dstr;
  103.     char *str_heap;
  104.     char *case_str;
  105.     int i, case_no, case_str_len;
  106.    
  107.     scanf("%d",&t_cases);
  108.     for(case_no = 0; case_no < t_cases; case_no++)
  109.     {
  110.         scanf("%d",&c_no);
  111.         constr = (char **)malloc(sizeof(char *)*c_no);
  112.         for(i = 0; i < c_no; i++)
  113.         {
  114.             str_heap = (char *)malloc(sizeof(char) * 3);
  115.             scanf("%s",str_heap);
  116.             constr[i] = str_heap;
  117.         }
  118.        
  119.         scanf("%d",&d_no);
  120.         dstr = (char **)malloc(sizeof(char *)*d_no);
  121.         for(i = 0; i < d_no; i++)
  122.         {
  123.             str_heap = (char *)malloc(sizeof(char) * 2);
  124.             scanf("%s",str_heap);
  125.             dstr[i] = str_heap;
  126.         }
  127.        
  128.         scanf("%d",&case_str_len);
  129.         str_heap = (char *)malloc(sizeof(char) * N_MAX);
  130.         scanf("%s",str_heap);
  131.         case_str = str_heap;
  132.        
  133.         process(case_no, constr, c_no, dstr, d_no, case_str);
  134.        
  135.         //TODO: free all heap
  136.     }
  137.    
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement