c - Incorrect code to check if a word can be made of smaller given words (word break) -
incorrect code check if word can made of smaller given words (word break).this code wrote above mentioned problem, online judge declares incorrect, possible reasons? , how should modify code?
#include <stdio.h> #include <stdlib.h> #include <string.h> /* node structure */ typedef struct node { int letter[26]; struct node* next[26]; int is_word; } node; /* create node */ node* getnode(void) { node* p = malloc(sizeof(node)); int i; (i = 0; < 1004; i++) { p->letter[i] = 0; p->next[i] = null; } p->is_word = 0; return p; } /* make dictionary */ void fill_dictionary(char word[], node* start) { int len = strlen(word), i; node* temp = start; (i = 0; < len; i++) { if (temp->letter[word[i] % 'a'] == 0) { temp->letter[word[i] % 'a'] = 1; temp->next[word[i] % 'a'] = getnode(); temp = temp->next[word[i] % 'a']; } else { temp = temp->next[word[i] % 'a']; } } temp->is_word = 1; return; } int spell_check(char line[100003], node* start) { int len = strlen(line), i, flag = 0; node* temp = start; (i = 0; < len; i++) { if (temp->letter[line[i] % 'a'] == 0) { return 1; } else { temp = temp->next[line[i] % 'a']; flag = 0; if (temp->is_word == 1) { flag = 1; temp = start; } } } if (flag == 1) { return 0; } else { return 1; } } int main(void) { int n, i, ans, m; scanf("%d %d", &n,&m); // no. of words in dictionary node* start = getnode(); (i = 0; < n; i++) { char word[11]; // max length of dictionary word scanf("%s", word); fill_dictionary(word, start); } scanf("%d", &n); // no. of lines checked (i = 0; < n; i++) { char line[100003]; // max length of line scanf("%s", line); ans = spell_check(line, start); if (ans == 0) { printf("yes\n"); } else { printf("no\n"); } } return 0; }
here's 1 way to it. compiles , runs. displays parsed result. tries read dictionary file called "dictionary.text" in current directory. can change put dictionary wherever want. commented heavily understand has subtle c things may need think , figure out. 1 bit of advice: name everything in program extremely accurately is/does possible (but reasonably succinct). immensely when trying debug or figure out did wrong. careless names make code confusing , hard debug.
good luck!
example:
$ gcc -o wordsplitter wordsplitter.c
$ wordsplitter xyzhellogoodbyefoodogcatpigcarwhereareyouhorse
xyz "hello" "goodbye" foo "dog" "cat" pigcar "where" "are" "you" horse
#include <stdio.h> #include <stdlib.h> #include <string.h> #define dictionary_filepath "dictionary.txt" #define max_word_size 100 /* * error codes (usually put in header file , included) */ #define success 0 #define file_not_found -1 #define out_of_memory -2 typedef struct word { struct word *next; char *word; } word_t; word_t *dictionarylisthead = null; typedef struct wordsubcomponent { struct wordsubcomponent *next; char *text; int isdictionaryword; } wordsubcomponent_t; int loaddictionaryfromfile(char *filename, word_t **listhead) { char wordfromfile[max_word_size]; word_t *lastwordstored = null; file *dictionaryfile = fopen(filename, "r"); if (dictionaryfile == null) { return file_not_found; } while(fgets(wordfromfile, sizeof(wordfromfile), dictionaryfile)) { word_t *newdictionarywordnode; if ((newdictionarywordnode = calloc(sizeof(word_t), 1)) == null) { // calloc automatically zeroes memory return out_of_memory; } char *cp = strchr(wordfromfile, '\n'); if (cp != null) *cp = '\0'; // rid of trailing \n newdictionarywordnode->word = strdup(wordfromfile); if (*listhead == null) { lastwordstored = *listhead = newdictionarywordnode; } else { lastwordstored = lastwordstored->next = newdictionarywordnode; } } fclose(dictionaryfile); return success; } wordsubcomponent_t *newsubcomponent() { wordsubcomponent_t *subcomp = null; if ((subcomp = calloc(sizeof(wordsubcomponent_t), 1)) != null) { subcomp->text = strdup(""); // seed empty string (instead of null) can append } else { fprintf(stderr, "out of memory (fatal). program exiting\n"); exit(-1); } return subcomp; } /* * returns linked list of word subcomponents given word, split around dictionary words */ wordsubcomponent_t *getwordsubcomponents(char *wordtoparse, word_t *listhead) { wordsubcomponent_t *subcomponents, *currsubcomp; subcomponents = currsubcomp = newsubcomponent(); (char *cp = wordtoparse; cp < wordtoparse + strlen(wordtoparse);) { // exit when cp gets end of word parse. int matchflag = 0; (word_t *wordnode = listhead; wordnode != null; wordnode = wordnode->next) { if (strncasecmp(cp, wordnode->word, strlen(wordnode->word)) == 0) { // prefix of cur. ptr dict word. if (strlen(currsubcomp->text) != 0) // detected non-dict text in subcomp. currsubcomp = currsubcomp->next = newsubcomponent(); // leave in list & add new subcomp dict word. currsubcomp->text = wordnode->word; // save dict-word in subcomp currsubcomp->isdictionaryword = 1; currsubcomp = currsubcomp->next = newsubcomponent(); // dict-word in list, new subcomp cp += strlen(wordnode->word); // advance cp past extracted dict-word matchflag = 1; break; // break out of inner-loop } } if (!matchflag) { // no dict-word found @ cp char onenullterminatedletter[2] = { *cp++, '\0' }; // put 1st ltr null-terminated string & adv cp. strcat(currsubcomp->text, onenullterminatedletter); // append letter-as-string curr subcomp } } return subcomponents; } void dumpdictionary(word_t *listhead) { printf("\nlist of dictionary words:\n"); printf("----------------\n"); (word_t *wordnode = listhead; wordnode != null; wordnode = wordnode->next) { printf(" %s\n", wordnode->word); } printf("----------------\n\n"); } int main(int argc, char **argv) { int status; if ((status = loaddictionaryfromfile(dictionary_filepath, &dictionarylisthead)) < 0) { switch(status) { case file_not_found: fprintf(stderr, "error accessing dictionary: %s\n", argv[0]); break; case out_of_memory: fprintf(stderr, "out of memory"); break; } return exit_failure; } /* * load dictionary first can show them list of words if didn't * pass in command line argument word parse. */ if (argc < 2) { fprintf(stderr, "usage: %s <word_to_parse>\n\n", argv[0]); dumpdictionary(dictionarylisthead); return exit_failure; } wordsubcomponent_t *subcomp = getwordsubcomponents(argv[1], dictionarylisthead); while(subcomp != null && strlen(subcomp->text) > 0) { if (subcomp->isdictionaryword) printf("\"%s\" ", subcomp->text); else printf("%s ", subcomp->text); subcomp = subcomp->next; } printf("\n"); return exit_success; }
Comments
Post a Comment