HDU 1075 What Are You Talking About(Trie的应用)

本文介绍了一个使用Trie树结构实现的火星文翻译器。该翻译器能够将一种虚构的火星语言翻译成英文,通过构建Trie树来存储词汇及其翻译,并实现了查找和更新功能。

What Are You Talking About

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 20680    Accepted Submission(s): 6852

Problem Description
Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book into English. Can you help him?
 

 

Input
The problem has only one test case, the test case consists of two parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string "START", this string should be ignored, then some lines follow, each line contains two strings, the first one is a word in English, the second one is the corresponding word in Martian's language. A line with a single string "END" indicates the end of the directory part, and this string should be ignored. The book part starts with a single line contains a string "START", this string should be ignored, then an article written in Martian's language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should translate it and write the new word into your translation, if you can't find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. Space(' '), tab('\t'), enter('\n') and all the punctuation should not be translated. A line with a single string "END" indicates the end of the book part, and that's also the end of the input. All the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters.
 

 

Output
In this problem, you have to output the translation of the history book.
 

 

Sample Input
START
from fiwo
hello difh
mars riwosf
earth fnnvk
like fiiwj
END
START
difh, i'm fiwo riwosf.
i fiiwj fnnvk!
END
 

 

Sample Output
hello, i'm from mars.
i like earth!
Hint
Huge input, scanf is recommended.
 

题目链接:HDU 1075

 

结构体Trie中用flag=1表示当前点是否是某个单词的结尾处(防止出现单词a为单词b的前缀但是却被b覆盖的情况,比如diff对应bbb,但是此时diff的前缀dif显然是不存在的),若为结尾,则flag赋值为1,然后把对应的翻译单词赋值给这个节点的s[]。

查询的是否看查完之后的flag是否为1,如果为1则存在该单词,否则flag为0或根本查不到,则返回NULL

一开始智障了把每个点flag都设为1狂WA……

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=26;
const int M=100;
struct Trie
{
    Trie *nxt[N];
    int flag;
    char s[M];
    Trie()
    {
        CLR(nxt,0);
        CLR(s,0);
        flag=0;
    }
};
Trie *L=new Trie();
void update(char s[],char earth[])
{
    int len=strlen(s);
    int indx;
    Trie *cur=L;
    for (int i=0; i<len; ++i)
    {
        indx=s[i]-'a';
        if(cur->nxt[indx])
            cur=cur->nxt[indx];
        else
        {
            Trie *one=new Trie();
            cur->nxt[indx]=one;
            cur=one;
        }
    }
    strcpy(cur->s,earth);
    cur->flag=1;
}
char* Find(char s[])
{
    int len=strlen(s);
    int indx;
    Trie *cur=L;
    for (int i=0; i<len; ++i)
    {
        indx=s[i]-'a';
        if(cur->nxt[indx]==NULL)
            return NULL;
        cur=cur->nxt[indx];
    }
    return cur->flag?cur->s:NULL;
}
char from[M],to[M];
char tot[1000010];
char temp[M];
int main(void)
{
    int i;
    scanf("%s",from);
    getchar();
    while (~scanf("%s",to)&&strcmp(to,"END"))
    {
        scanf("%s",from);
        update(from,to);
    }
    getchar();

    gets(from);
    while (gets(tot)&&strcmp(tot,"END"))
    {
        int len=strlen(tot);
        int k=0;
        CLR(temp,0);
        for (i=0; i<len; ++i)
        {
            if(islower(tot[i]))
                temp[k++]=tot[i];
            else
            {
                char *one=Find(temp);
                if(k)
                    printf("%s",one==NULL?temp:one);
                putchar(tot[i]);
                CLR(temp,0);
                k=0;
            }
        }
        if(k)
        {
            char *one=Find(temp);
            printf("%s",one==NULL?temp:one);
        }
        putchar('\n');
    }
    return 0;
}

转载于:https://www.cnblogs.com/Blackops/p/5908836.html

标题基于Python的自主学习系统后端设计与实现AI更换标题第1章引言介绍自主学习系统的研究背景、意义、现状以及本文的研究方法和创新点。1.1研究背景与意义阐述自主学习系统在教育技术领域的重要性和应用价值。1.2国内外研究现状分析国内外在自主学习系统后端技术方面的研究进展。1.3研究方法与创新点概述本文采用Python技术栈的设计方法和系统创新点。第2章相关理论与技术总结自主学习系统后端开发的相关理论和技术基础。2.1自主学习系统理论阐述自主学习系统的定义、特征和理论基础。2.2Python后端技术栈介绍DjangoFlask等Python后端框架及其适用场景。2.3数据库技术讨论关系型和非关系型数据库在系统中的应用方案。第3章系统设计与实现详细介绍自主学习系统后端的设计方案和实现过程。3.1系统架构设计提出基于微服务的系统架构设计方案。3.2核心模块设计详细说明用户管理、学习资源管理、进度跟踪等核心模块设计。3.3关键技术实现阐述个性化推荐算法、学习行为分析等关键技术的实现。第4章系统测试与评估对系统进行功能测试和性能评估。4.1测试环境与方法介绍测试环境配置和采用的测试方法。4.2功能测试结果展示各功能模块的测试结果和问题修复情况。4.3性能评估分析分析系统在高并发等场景下的性能表现。第5章结论与展望总结研究成果并提出未来改进方向。5.1研究结论概括系统设计的主要成果和技术创新。5.2未来展望指出系统局限性并提出后续优化方向。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值