zoukankan      html  css  js  c++  java
  • 字典树 HDU 1075 What Are You Talking About

    http://acm.hdu.edu.cn/showproblem.php?pid=1075

    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<ctype.h>
    #include<stdio.h>
    #include<stdlib.h>
    using namespace std;

    struct
    node
    {

        int
    flag;
        char
    str[15];
        node *next[26];
       /* node()
        {
            memset(next, NULL, sizeof(next));
            flag=0;
        }*/

        node():flag(0){memset(next, NULL, sizeof(next));};
    };

    node *head=new node();

    void
    connect(char *a, char *b)
    {

        node *p=head;
        //node *p=new node();
        for(int i=0; a[i]; i++)
        {

            int
    k=a[i]-'a';
            if
    (p->next[k]==NULL)
                p->next[k]=new node();
            p=p->next[k];
        }

        p->flag=1;
        strcpy(p->str, b);
    }

    char
    *query(char *s)
    {

        node *p=head;
        for
    (int i=0; s[i]; i++)
        {

            int
    k=s[i]-'a';
            if
    (p->next[k]==NULL)
                return
    NULL;
            p=p->next[k];
        }

        if
    (p->flag==1)
            return
    p->str;
        else
            return
    NULL;
    }

    int
    main()
    {

        char
    m[15], e[15], b[3030], f[15];
        gets(m);
        while
    (scanf("%s", m), strcmp(m, "END")!=0)
        {

            scanf("%s", e);
            connect(e, m);
        }

        scanf("%s", b);
        getchar();
        while
    (gets(b), strcmp(b, "END")!=0)
        {

            int
    i, j;

            for
    (i=0; b[i]; i++)
            {

                j=0;
                while
    (b[i]>='a'&&b[i]<='z')
                {

                    f[j++]=b[i];
                    i++;
                }

                f[j]=0;
                char
    *k=query(f);
                /*char k[15];
                 k=query(f);*/

                if
    (k==0)
                    printf("%s", f);
                else

                    printf("%s", k);
                printf("%c", b[i]);
            }

            printf(" ");
        }

        return
    0;
    }

  • 相关阅读:
    关于W3Cschool定义的设计模式--常用的9种设计模式的介绍
    正则得介绍和使用——表单验证
    DOM的高级操作-一种JS控制元素的视觉假象
    如何理解JS中this指向的问题
    Vulkan中的实时软阴影与硬件优化
    TensorFlow Distribution(分布式中的数据读取和训练)
    TensorFlow白皮书
    TensorFlow Data模块
    新闻标签提取的评价方法
    基于TF-IDF的新闻标签提取
  • 原文地址:https://www.cnblogs.com/wazqWAZQ1/p/4692933.html
Copyright © 2011-2022 走看看