字典树 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("\n");
    }

    return
0;
}

时间: 2024-12-13 14:24:16

字典树 HDU 1075 What Are You Talking About的相关文章

字典树 HDU 1251 统计难题

http://acm.hdu.edu.cn/showproblem.php?pid=1251 #include<iostream> #include<algorithm> #include<stdio.h> using namespace std; struct node { int sum; node *next[26]; }; void buildtrietree(node *head, char s[]) { node *p=new node(); p=head;

HDU 1075 What Are You Talking About(字典树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075 题意:根据词典翻译语句. 思路:裸的字典树.每个节点存以该节点为结尾的对应的单词. 代码: #include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <algorithm> #include <string> #incl

HDU 1075 map or 字典树

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)Total Submission(s): 12773    Accepted Submission(s): 4069 Problem Description Ignatius is so lucky that he met a Martian yesterday. But

hdu 1075 字典树

// hdu 1075 字典树 // // 题目大意: // // 给你一个字典,即有两个字符串,一个是英文,一个是火星文,然后 // 输入一段火星文,要你翻译成英文. // // 解题思路: // // 字典树,查字典嘛,有就输出查到的,没有原样输出.将火星文插入到 // 字典树中,然后在字典输中查找.找到了,输出对应的英文,否则,原样输 // 出. // // 感悟: // // 题目确实很简单,但是,没告诉数据范围啊,导致我一直RE,原来单词 // 可能对应很长的英文啊,找人家ac的开数组

hdu 1075 What Are You Talking About 字典树 trie

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others) Total Submission(s): 14703    Accepted Submission(s): 4724 Problem Description Ignatius is so lucky that he met a Martian yesterday. But

hdu 1075(字典树)

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)Total Submission(s): 21658    Accepted Submission(s): 7228 Problem Description Ignatius is so lucky that he met a Martian yesterday. But

HDU 1075 What Are You Talking About(map或字典树)

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)Total Submission(s): 24624    Accepted Submission(s): 8280 Problem Description Ignatius is so lucky that he met a Martian yesterday. But

hdu 1075:What Are You Talking About(字典树,经典题)

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)Total Submission(s): 12617    Accepted Submission(s): 4031 Problem Description Ignatius is so lucky that he met a Martian yesterday. But

HDU 1075 What Are You Talking About (Trie树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075 map可以过...我上的字典树,小bug有点尴尬,题目没有明确给出数据范围也是无奈. 贡献了几次RE 一次WA.尴尬.discuss里面有个说注意前缀的到是给了点tip.总体来说不错 代码: 1 #define _CRT_SECURE_NO_WARNINGS 2 #include <functional> 3 #include <algorithm> 4 #include <