codevs 4189 字典

Posted ZlycerQan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codevs 4189 字典相关的知识,希望对你有一定的参考价值。

二次联通门 : codevs 4189 字典

 

 

/*
    codevs 4189 字典 
    
    Trie树
    
    裸题
    用指针写的..
     
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>

void read (int &now)
{
    now = 0;
    register char word = getchar ();
    while (word < 0 || word > 9)
        word = getchar ();
    while (word >= 0 && word <= 9)
    {
        now = now * 10 + word - 0;
        word = getchar ();
    }
}

struct Trie_Type
{

    struct Trie
    {
        Trie *next[26];
        bool End;
        bool flag;
    };
        
    Trie Root;
    
    void Insert (char *line)
    {
        int Length = strlen (line);
        Trie *now = &Root;
        Trie *res;
        for (int i = 0; i < Length; i++)
        {
            int name = line[i] - a;
            if (now->next[name] == NULL)
            {
                res = (Trie *) malloc (sizeof Root);
                res->flag = true;
                for (int j = 0; j < 26; j++)
                    res->next[j] = NULL;
                now->next[name] = res;
                now = now->next[name];
            }
            else
            {
                now->next[name]->flag = true;
                now = now->next[name];
            }
        }
    }
    
    void Clear ()
    {
        for (int i = 0; i < 26; i++)
            Root.next[i] = NULL;
    }
    
    bool Search (char *line)
    {
        int Length = strlen (line);
        Trie *now = &Root;
        int name;
        for (int i = 0; i < Length; i++)
        {
            name = line[i] - a;
            now = now->next[name];
            if (now == NULL)
                return false;
        }    
        return true;
    }
};

Trie_Type Make;
int N;

int main (int argc, char *argv[])
{
    read (N);
    Make.Clear ();
    register char word[10];
    for (int i = 1; i <= N; i++)
    {
        scanf ("%s", word);
        Make.Insert (word); 
    
    }
    read (N);
    while (N--)
    {
        scanf ("%s", word);
        printf (Make.Search (word) ? "YES" : "NO");
        putchar (\n);
    }
    return 0;
}

 

以上是关于codevs 4189 字典的主要内容,如果未能解决你的问题,请参考以下文章

codevs 4189 字典

AC日记——字典 codevs 4189

codevs——4189 字典

codeVS 4189 字典

CODEVS 4189 (前缀是否出现)

4189 字典