码迷,mamicode.com
首页 > 其他好文 > 详细

codevs 4189 字典

时间:2017-04-23 20:16:26      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:blank   div   target   ref   strlen   can   name   putc   make   

二次联通门 : 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 字典

标签:blank   div   target   ref   strlen   can   name   putc   make   

原文地址:http://www.cnblogs.com/ZlycerQan/p/6753645.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!