求助,全WA,总是说line too short,又无法下载用例
查看原帖
求助,全WA,总是说line too short,又无法下载用例
505164
iamdave楼主2021/4/16 12:01

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

#define N 1000

typedef struct {
    char c;
    unsigned char valid;
}Letter;

typedef struct Node{
    char end;
    struct Node *next[26];
}Node;

typedef struct {
    unsigned short article_id;
    Node root;
}RootNode;


RootNode g_text[N] = {0};

void AddWordToTree(Node *root, char word[])
{
    Node *tree = root;
    unsigned int wordLen = strlen(word);
    for (unsigned int k = 0; k < wordLen; k++) {
        // 记录字符
        char c = word[k];
        unsigned int index = c - 'a';
        if (tree->next[index] == 0) {
            tree->next[index] = malloc(sizeof(Node));
            memset(tree->next[index], 0, sizeof(Node));
            // printf("%p\n", tree->next[index]);
        }
        tree = tree->next[index];
    }
    tree->end = 1;
}

void ReleaseMemroy(Node *tree)
{
    if (tree == 0) {
        return;
    }
    for (unsigned int i = 0; i < 26; i++) {
        if (tree->next[i] != 0) {
            ReleaseMemroy(tree->next[i]);
        }
    }
    // printf("%p\n", tree);
    free(tree);
    tree = 0;
}

void QueryWord(unsigned int n, char word[], unsigned int index, int num)
{   
    int first = 0;
    unsigned int wordLen = strlen(word);
    for (unsigned int i = 0; i < n; i++) {
        Node *tree = &g_text[i].root;
        unsigned int k = 0;
        for (; k < wordLen; k++) {
            // 记录字符
            char c = word[k];
            unsigned int index = c - 'a';
            if (tree->next[index] != 0) {
                tree = tree->next[index];
            } else {
                // 不匹配,没找到
                break;
            }
        }
        if (k >= wordLen && tree->end) {
            first++;
            if (first > 1) {
                printf(" ");
            }
            printf("%d", g_text[i].article_id);
        }
    }
    
    if (first > 0) {
        if (index < num - 1) {
            printf("\n");
        }
    } else {
        // 没找到,输出空行
        printf("\n");
    }
}

int main()
{
    unsigned int n;
    scanf("%u", &n);

    unsigned text_id = 0;
    char word[20];
    for (unsigned int i = 0; i < n; i++) {
        unsigned int word_num;
        scanf("%u", &word_num);

        text_id++;
        g_text[i].article_id = text_id;
        
        for (int m = 0; m < word_num; m++) {
            scanf("%s", word);
            AddWordToTree(&g_text[i].root, word);
        }
    }

    int query_num;
    scanf("%d", &query_num);
    for (unsigned int i = 0; i < query_num; i++) {
        scanf("%s", word);
        QueryWord(n, word, i, query_num);
        // if (i < query_num - 1) {
        //     printf("\n");
        // }
    }

    for (unsigned int i = 0; i < n; i++) {
        for (int m = 0; m < 26; m++) {
            ReleaseMemroy(g_text[i].root.next[m]);
        }
    }

    return 0;
}
2021/4/16 12:01
加载中...