zoukankan      html  css  js  c++  java
  • POJ 2503 Babelfish

    字典树,WA;

    ----------------------------------------------------

    # include <stdio.h>
    # include <string.h>
    
    # define LEN 12
    
    char buf[2 * LEN], str[LEN];
    
    struct word{
        char a[LEN];
    } ;
    
    struct tree{
        tree *node[26];
        char bj;
        word *pw;
        tree() {
            for (int i = 0; i < 26; ++i)
                node[i] = NULL;
            bj = 1;
            pw = NULL;
        }
    } *root;
    
    void build(tree *p, char *s)
    {
        char c = (*s) - 'a';
    
        if ((p->node[c]) == NULL)
        {
            p->node[c] = new tree;
        }
        p = p->node[c];
        if (s[2]) build(p, s+1);
        else
        {
            p->bj = 0;
            if (p->pw == NULL){
            p->pw = new word;
            sscanf(buf, "%s", p->pw);}
        }
    }
    
    void visit(tree *p, char *s)
    {
        char c = (*s) - 'a';
    
        p = p->node[c];
        if (p == NULL) puts("eh");
        else if (s[2]) visit(p, s+1);
        else if (p->bj==0 && !s[2]) puts(p->pw->a);
        else puts("eh");
    }
    
    void release(tree *p)
    {
        for (int i = 0; i < 26; ++i)
            if (p->node[i]) release(p->node[i]);
        delete p->pw;
        delete p;
    }
    
    void init(void)
    {
        root = new tree;
        while (1)
        {
            gets(buf);
            if (!buf[0]) return ;
            sscanf(buf, "%*s%s", str);
            build(root, str);
        }
    }
    
    void solve(void)
    {
        while (~scanf("%s", str))
        {
            visit(root, str);
        }
        release(root);
    }
    
    int main()
    {    
        init();
        solve();
    
        return 0;
    }

    ----------------------------------------------------

  • 相关阅读:
    Node.js:事件循环
    Node.js:回调函数
    Node.js:REPL(交互式解释器)
    Node.js:NPM 使用介绍
    Node.js:创建第一个应用
    Node.js:安装配置
    Node.js:教程
    Node.js:目录
    Node.js:template
    虚拟化之xenserver
  • 原文地址:https://www.cnblogs.com/JMDWQ/p/2588488.html
Copyright © 2011-2022 走看看