Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Magicka
- sid@whackylabs.com
- */
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- #define C_MAX 40
- #define D_MAX 30
- #define N_MAX 105
- #define DEBUG 0
- int is_reacting(int c_strs_len, char **c_strs, char x, char y)
- {
- int i;
- char *c_str;
- int react = -1;
- for(i = 0 ; i < c_strs_len && react < 0; i++)
- {
- c_str = c_strs[i];
- if(x == '.' || y == '.')
- continue;
- if((c_str[0] == x && c_str[1] == y) || (c_str[0] == y && c_str[1] == x))
- react = i;
- }
- return react;
- }
- void process(int case_no, char **constr, int c_no, char **dstr, int d_no, char *case_str)
- {
- char *list;
- int i,j, k, match;
- int print_comma = 0;
- if(DEBUG)
- {
- printf("\nprocessing case #: %d\n",case_no+1);
- printf("constructors:\n");
- for(i = 0; i < c_no; i++)
- printf("%s\n",constr[i]);
- printf("destructors:\n");
- for(i = 0; i < d_no; i++)
- printf("%s\n",dstr[i]);
- printf("case-str: %s\n",case_str);
- printf("\n");
- }
- list = (char *)malloc(sizeof(char) * strlen(case_str));
- strcpy(list,case_str);
- for(i = 1; i < strlen(list); i++)
- {
- //construction
- if((match = is_reacting(c_no, constr,list[i],list[i-1])) >= 0)
- {
- list[i] = constr[match][2];
- list[i-1] = '.';
- }
- else
- {
- //destruction
- for(j = i-1; j >= 0; j--)
- {
- if((match = is_reacting(d_no, dstr,list[i],list[j])) >= 0)
- {
- for(k = j; k <= i; k++)
- list[k] = '.';
- }
- }
- }
- if(DEBUG) printf("-- HERE :%d --\n",i);
- }
- //print
- printf("Case #%d: [",case_no+1);
- for(i = 0; i < strlen(list);)
- {
- if(list[i] == '.')
- {
- i++;
- continue;
- }
- if(print_comma)
- printf(", ");
- else
- print_comma = 1;
- printf("%c",list[i++]);
- }
- printf("]\n");
- free(list);
- }
- int main(void)
- {
- int t_cases, c_no, d_no;
- char **constr;
- char **dstr;
- char *str_heap;
- char *case_str;
- int i, case_no, case_str_len;
- scanf("%d",&t_cases);
- for(case_no = 0; case_no < t_cases; case_no++)
- {
- scanf("%d",&c_no);
- constr = (char **)malloc(sizeof(char *)*c_no);
- for(i = 0; i < c_no; i++)
- {
- str_heap = (char *)malloc(sizeof(char) * 3);
- scanf("%s",str_heap);
- constr[i] = str_heap;
- }
- scanf("%d",&d_no);
- dstr = (char **)malloc(sizeof(char *)*d_no);
- for(i = 0; i < d_no; i++)
- {
- str_heap = (char *)malloc(sizeof(char) * 2);
- scanf("%s",str_heap);
- dstr[i] = str_heap;
- }
- scanf("%d",&case_str_len);
- str_heap = (char *)malloc(sizeof(char) * N_MAX);
- scanf("%s",str_heap);
- case_str = str_heap;
- process(case_no, constr, c_no, dstr, d_no, case_str);
- //TODO: free all heap
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement