Arrays

  • + 0 comments

    did you get any resolution / suggestions on this, i have a problem with the same two test cases, says i'm segfaulting on 16 & 17 but gcc version 6.3.1 20161221 (Red Hat 6.3.1-1) (GCC) with options -g & -Wall disagrees. my code:

    include

    include

    include

    include

    include

    include

    include

    struct node{ char *word; int count; struct node *next; };

    void mainlist(char *word, struct node **list){ struct node *tmp = *list; struct node *prev = 0; if(!tmp){ (*list) = calloc(1,sizeof(*list)); (*list)->word = word; (*list)->count = 1; (*list)->next = 0; return; } while(tmp){ if(strcmp(tmp->word,word)==0){ (tmp->count)++; return; } prev = tmp; tmp = tmp->next; } prev->next = calloc(1, sizeof(prev->next)); prev->next->word = word; prev->next->count = 1; prev->next->next = 0; return; }

    struct node *inlist(char *word, struct node **list){ struct node *n = *list; while(n){
    if(strcmp(n->word, word)==0){ return n; } n = n->next; } return 0; }

    int main(){ int m; int n; scanf("%d %d",&m,&n);

    struct node *ml[256] = {0};
    struct node *tmp = 0;
    int c = 0;
    
    char* *magazine = malloc(sizeof(char*) * m);
    for(int magazine_i = 0; magazine_i < m; magazine_i++){
       magazine[magazine_i] = (char *)malloc(10240 * sizeof(char));
       scanf("%s",magazine[magazine_i]);
    }
    
    for(int i = 0; i < m; i++){
        c = (int)magazine[i][0];
        mainlist(magazine[i],&ml[c]);
    }
    
    char* *ransom = malloc(sizeof(char*) * n);
    for(int ransom_i = 0; ransom_i < n; ransom_i++){
       ransom[ransom_i] = (char *)malloc(10240 * sizeof(char));
       scanf("%s",ransom[ransom_i]);
    }
    
    for(int j = 0; j < n; j++){
        c = (int)ransom[j][0];
        tmp = inlist(ransom[j],&ml[c]);
        if(!tmp){
            printf("No\n");
            return 0;
        }else{
            (tmp->count)--;
            if((tmp->count) < 0){
                printf("No\n");
                return 0;
            }
        }
    }
    printf("Yes\n");
    return 0;
    

    }