Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 检查单词是否可以由较小的给定单词组成的代码不正确(分词)_C_Algorithm_Trie - Fatal编程技术网

C 检查单词是否可以由较小的给定单词组成的代码不正确(分词)

C 检查单词是否可以由较小的给定单词组成的代码不正确(分词),c,algorithm,trie,C,Algorithm,Trie,检查一个单词是否可以由较小的给定单词组成(分词)的错误代码。这是我为上述问题编写的代码,但是一位在线法官宣布它不正确,可能的原因是什么?我应该如何修改我的代码 #include <stdio.h> #include <stdlib.h> #include <string.h> /* Node structure */ typedef struct node { int letter[26]; struct node* next[26];

检查一个单词是否可以由较小的给定单词组成(分词)的错误代码。这是我为上述问题编写的代码,但是一位在线法官宣布它不正确,可能的原因是什么?我应该如何修改我的代码

#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;
    for (i = 0; i < 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;

    for (i = 0; i < 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;

    for (i = 0; i < 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();

    for (i = 0; i < n; i++) {
        char word[11];      // max length of dictionary word
        scanf("%s", word);
        fill_dictionary(word, start);
    }

    scanf("%d", &n);        // no. of lines to be checked

    for (i = 0; i < n; i++) {
        char line[100003];  // max length of a line
        scanf("%s", line);
        ans = spell_check(line, start);

        if (ans == 0) {
            printf("YES\n");
        } else {
            printf("NO\n");
        }
    }

    return 0;
}
#包括
#包括
#包括
/*节点结构*/
类型定义结构节点{
int字母[26];
结构节点*next[26];
int是_字;
}节点;
/*创建节点*/
节点*getnode(无效){
node*p=malloc(sizeof(node));
int i;
对于(i=0;i<1004;i++){
p->字母[i]=0;
p->next[i]=NULL;
}
p->is_word=0;
返回p;
}
/*编字典*/
void fill_字典(字符单词[],节点*开始){
int len=strlen(字),i;
节点*温度=开始;
对于(i=0;i字母[word[i]%a']==0){
临时->字母[word[i]%a']=1;
temp->next[word[i]'a']=getnode();
temp=temp->next[word[i]%a'];
}否则{
temp=temp->next[word[i]%a'];
}
}
temp->is_word=1;
返回;
}
int拼写检查(字符行[100003],节点*开始){
int len=strlen(线),i,flag=0;
节点*温度=开始;
对于(i=0;i字母[行[i]%a']==0){
返回1;
}否则{
temp=temp->next[行[i]%a'];
flag=0;
如果(temp->is_word==1){
flag=1;
温度=启动;
}
}
}
如果(标志==1){
返回0;
}否则{
返回1;
}
}
内部主(空){
int n,i,ans,m;
scanf(“%d%d”,&n,&m);//词典中的单词数
node*start=getnode();
对于(i=0;i
这里有一种方法。这将编译并运行。它显示解析的结果。它尝试从当前目录中名为“dictionary.text”的文件中读取字典。你可以把它改成把字典放在你想放的任何地方。我对它进行了大量的评论,以帮助您理解它,但它有一些微妙的C语言方面的东西,您可能需要真正思考和弄清楚。一点建议:尽可能准确地(但相当简洁地)说出程序中的所有内容。这将极大地帮助你调试或找出你做错了什么。粗心的名称确实会使代码混乱,难以调试

祝你好运

例如:

$gcc-o字拆分器字拆分器.c

$wordsplitter xyzhellogoodbyefoodcatpigcar您的马在哪里

xyz“你好”“再见”foo“狗”“猫”pigcar“你”在哪里“马”

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define DICTIONARY_FILEPATH  "dictionary.txt"
#define MAX_WORD_SIZE  100
/*
 * Error codes (usually this is put in a header file and 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'; // get 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 with empty string (instead of NULL) so we can append
    } else {
        fprintf(stderr, "out of memory (fatal). program exiting\n");
        exit(-1);
    }
    return subcomp;
}

/*
 * Returns an linked list of word subcomponents for the given word, split up around dictionary words
 */
wordsubcomponent_t *getWordSubcomponents(char *wordToParse, word_t *listhead) {
    wordsubcomponent_t *subcomponents, *currSubcomp;
    subcomponents = currSubcomp = newsubcomponent();
    for (char *cp = wordToParse; cp < wordToParse + strlen(wordToParse);) { // exit when cp gets to end of word to parse.
        int matchFlag = 0;
        for (word_t *wordNode = listhead; wordNode != NULL; wordNode = wordNode->next) {
            if (strncasecmp(cp, wordNode->word, strlen(wordNode->word)) == 0) { // prefix of cur. ptr is dict word.
                if (strlen(currSubcomp->text) != 0) // Detected non-dict text in subcomp.
                    currSubcomp = currSubcomp->next = newsubcomponent(); // leave in list & add new subcomp for dict word.
                currSubcomp->text = wordNode->word; // save dict-word in subcomp
                currSubcomp->isDictionaryWord = 1;
                currSubcomp = currSubcomp->next = newsubcomponent(); // dict-word in list, so get 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 at cp      
            char oneNullTerminatedLetter[2] = { *cp++, '\0' }; // put 1st ltr into NULL-terminated string & adv cp.         
            strcat(currSubcomp->text, oneNullTerminatedLetter); // append letter-as-string to curr subcomp
        }
    }
    return subcomponents;
}

void
dumpDictionary(word_t *listhead) {
    printf("\nList of dictionary words:\n");
    printf("----------------\n");
    for (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 so we can show them the list of words if they didn't
     * pass in a command line argument with the word to 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;
}
#包括
#包括
#包括
#定义DICTIONARY\u文件路径“DICTIONARY.txt”
#定义最大单词大小100
/*
*错误代码(通常放在头文件中并包含)
*/
#定义成功0
#定义未找到的文件\u-1
#定义内存中的\u-2
typedef结构字{
结构词*next;
字符*字;
}单词t;
word_t*DictionaryListad=NULL;
typedef结构字子组件{
结构字子组件*next;
字符*文本;
国际数字词典;
}单词子组件;
int
loadDictionaryFromFile(字符*文件名,单词**列表头)
{
char wordFromFile[最大单词大小];
word_t*lastWordStored=NULL;
FILE*dictionaryFile=fopen(文件名,“r”);
如果(dictionaryFile==NULL){
未找到返回文件;
}    
while(fgets(wordFromFile、sizeof(wordFromFile)、dictionaryFile)){
word_t*新建字典或节点;
如果((newDictionaryWordNode=calloc(sizeof(word_t),1))==NULL){//calloc会自动将内存归零
返回\u内存中的\u;
}
char*cp=strchr(wordFromFile,'\n');
如果(cp!=NULL)
*cp='\0';//去掉尾随\n
newDictionaryWordNode->word=strdup(wordFromFile);
如果(*listhead==NULL){
lastWordStored=*listhead=newDictionaryWordNode;
}否则{
lastWordStored=lastWordStored->next=newDictionaryWordNode;
}
}
fclose(字典文件);
回归成功;
}
wordsubcomponent\u t
*newsubcomponent(){
wordsubcomponent\u t*subcomponent=NULL;
如果((subcomponent=calloc(sizeof(wordsubcomponent_t),1))!=NULL){
subcomp->text=strdup(“”;//使用空字符串(而不是NULL)进行种子设定,以便可以追加
}否则{
fprintf(stderr,“内存不足(致命)。程序正在退出\n”);
出口(-1);
}
返回子comp;
}
/*
*返回给定单词的单词子组件的链接列表,并围绕字典中的单词进行拆分
*/
wordsubcomponent\u t*获取wordsubcomponents(char*wordToParse,word\u t*列表头){
wordsubcomponent\u t*subcomponents,*currensubcomponent;
subcomponents=CurrSubComponent=newsubcomponent();
for(char*cp=wordToParse;cpnext){
如果(strncasecmp(cp,wordNode->word,strlen(wordNode->word))==0){//cur.ptr的前缀是dict word。
if(strlen(currSubcomp->text)!=0)//在子comp中检测到非dict文本。
currSubcomp=currSubcomp->next=newsubcomponent();//留在列表中并为dict word添加新的子comp。
currSubcomp->text=wordNode->word;//在subcomp中保存dict单词
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
            fclose(dictionaryFile); // <-- Close the file pointer
            return OUT_OF_MEMORY;
        }
        char *cp = strchr(wordFromFile, '\n');
        if (cp != NULL)
            *cp = '\0'; // get rid of trailing \n

        newDictionaryWordNode->word = strdup(wordFromFile);     
        if (*listhead == NULL) {
            lastWordStored = *listhead = newDictionaryWordNode;
        } else {
            lastWordStored = lastWordStored->next = newDictionaryWordNode;
        }
    }
    fclose(dictionaryFile);
    return SUCCESS;
}