zoukankan      html  css  js  c++  java
  • HDU_1075 What Are You Talking About(Trie 树)

      题意:这题很好理解,给一个字典,上边有火星文对应的英语,让后输入一句话,把它翻译成英语。

      思路:定义Tire树时加一个存放字符串的数组s[],按字典上的火星文建立Trie树,在每一个火星文的末尾将对应的英文存到s[]中。查找时如果能在Trie树中找到,就将s[]对应的串取出来(至于怎么取,自己先想想。。。)分解输入的句子时注意细节。还有就是静态建立Trie树,我开始用动态,WA了。。。

    My Code:

    #include <iostream>
    #include
    <cstdio>
    #include
    <cstring>
    #include
    <cstdlib>

    using namespace std;

    const int N = 3007;

    struct node
    {
    int flag;
    char t[11];
    struct node * next[26];
    };

    node
    * newword()
    {
    int i;
    node
    * p = new node;
    p
    ->flag = 0;
    for(i = 0; i < 26; i++)
    p
    ->next[i] = NULL;
    return p;
    }

    void insert(node * root, char *s, char *s2)
    {
    node
    * p = root;
    int i, len = strlen(s), tmp;
    for(i = 0; i < len; i++)
    {
    tmp
    = s[i] - 'a';
    if(p->next[tmp] == NULL)
    p
    ->next[tmp] = newword();
    p
    = p->next[tmp];
    }
    p
    ->flag = 1;
    strcpy(p
    ->t, s2);
    }

    int search(node * root, char *s, char *tm)
    {
    node
    * p = root;
    int i, tmp, len = strlen(s);
    for(i = 0; i < len; i++)
    {
    tmp
    = s[i] - 'a';
    if(p->next[tmp] == NULL)
    return 0;
    p
    = p->next[tmp];
    }
    if(p->flag)
    {
    strcpy(tm, p
    ->t);
    return 1;
    }
    return 0;
    }

    void del(node * p)
    {
    if(p)
    {
    for(int i = 0; i < 26; i++)
    if(p->next[i])
    del(p
    ->next[i]);
    }
    free(p);
    p
    = NULL;
    }

    int main()
    {
    //freopen("data.in", "r", stdin);
    node * root;
    root
    = newword();
    char s[11], s2[11];
    while(scanf("%s", s) != EOF)
    {
    if(!strcmp(s, "END")) break;
    if(strcmp(s, "START"))
    {
    scanf(
    "%s", s2);
    insert(root, s2, s);
    }
    }
    getchar();
    char line[N], tm[11];
    while(gets(line) != NULL)
    {
    if(!strcmp(line, "END")) break;
    if(strcmp(line, "START"))
    {
    int len = strlen(line);
    int k = 0;
    memset(s,
    0, sizeof(s));
    for(int i = 0; i < len; i++)
    {

    if(islower(line[i]))
    {
    s[k
    ++] = line[i];
    if(i == len - 1)
    {
    memset(tm,
    0, sizeof(tm));
    if(search(root, s, tm))
    {
    int l = strlen(tm);
    for(int j = 0; j < l; j++)
    printf(
    "%c", tm[j]);
    }
    else
    {
    for(int j = 0; j < k; j++)
    printf(
    "%c", s[j]);
    }
    break;
    }
    }
    else
    {
    memset(tm,
    0, sizeof(tm));
    if(search(root, s, tm))
    {
    int l = strlen(tm);
    for(int j = 0; j < l; j++)
    printf(
    "%c", tm[j]);
    }
    else
    for(int j = 0; j < k; j++)
    printf(
    "%c", s[j]);
    printf(
    "%c", line[i]);
    k
    = 0;memset(s, 0, sizeof(s));
    }
    }
    putchar(
    '\n');
    }
    memset(line,
    0, sizeof(line));
    }
    del(root);
    return 0;
    }
  • 相关阅读:
    物质的物理属性·基础整理
    点分治
    洛谷 P3806 【模板】点分治1
    因式分解
    小石潭记
    反演原理及二项式反演
    《庄子》二则
    FFT快速傅里叶变换
    mysql 密码相关
    django基础
  • 原文地址:https://www.cnblogs.com/vongang/p/2147992.html
Copyright © 2011-2022 走看看