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.
[/hint]
#include<stdio.h>
#include<string.h>
#include<malloc.h>
typedef struct nnn
{
int flag;
char *traslat;
struct nnn *next[26];
}node;
node *builde()
{
node *p=(node*)malloc(sizeof(node));
p->flag=0;
for(int i=0;i<26;i++)
p->next[i]=NULL;
return p;
}
node *root;
void insert(char str[],char tras[])
{
node *p=root;
for(int i=0;str[i]!='\0';i++)
{
if(p->next[str[i]-'a']==NULL)
p->next[str[i]-'a']=builde();
p=p->next[str[i]-'a'];
}
p->flag=1;
int len=strlen(tras)+1;
p->traslat=(char*)malloc(len*sizeof(char));
strcpy(p->traslat,tras);
}
void trasl(char str[])
{
node *p=root;
for(int i=0;str[i]!='\0';i++)
{
if(p->next[str[i]-'a']==NULL)
{
printf("%s",str); return ;
}
p=p->next[str[i]-'a'];
}
if(p->flag)
printf("%s",p->traslat);
else
printf("%s",str);
}
int main()
{
char tras[15],str[3005],str1[15];
root=builde();
while(scanf("%s",tras)>0&&strcmp(tras,"END")!=0)
{
if(strcmp("START",tras)==0)continue;
scanf("%s",str);
insert(str,tras);
}
getchar();
while(gets(str),strcmp(str,"END")!=0)
{
if(strcmp("START",str)==0)continue;
int len=strlen(str);
for(int i=0,j=0;i<=len; i++)
{
if(str[i]>='a'&&str[i]<='z')
str1[j++]=str[i];
else
{
str1[j]='\0';
if(j) trasl(str1);
if(i==len||str[i]=='\n')printf("\n");
else if(str[i]=='\t') printf("\t");
else printf("%c",str[i]);
j=0;
}
}
}
}

hdu1075What Are You Talking About (字典树)的更多相关文章

  1. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  2. [LeetCode] Implement Trie (Prefix Tree) 实现字典树(前缀树)

    Implement a trie with insert, search, and startsWith methods. Note:You may assume that all inputs ar ...

  3. 字典树+博弈 CF 455B A Lot of Games(接龙游戏)

    题目链接 题意: A和B轮流在建造一个字,每次添加一个字符,要求是给定的n个串的某一个的前缀,不能添加字符的人输掉游戏,输掉的人先手下一轮的游戏.问A先手,经过k轮游戏,最后胜利的人是谁. 思路: 很 ...

  4. 萌新笔记——C++里创建 Trie字典树(中文词典)(一)(插入、遍历)

    萌新做词典第一篇,做得不好,还请指正,谢谢大佬! 写了一个词典,用到了Trie字典树. 写这个词典的目的,一个是为了压缩一些数据,另一个是为了尝试搜索提示,就像在谷歌搜索的时候,打出某个关键字,会提示 ...

  5. 山东第一届省赛1001 Phone Number(字典树)

    Phone Number Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 We know that if a phone numb ...

  6. 字典树 - A Poet Computer

    The ACM team is working on an AI project called (Eih Eye Three) that allows computers to write poems ...

  7. trie字典树详解及应用

    原文链接    http://www.cnblogs.com/freewater/archive/2012/09/11/2680480.html Trie树详解及其应用   一.知识简介        ...

  8. HDU1671 字典树

    Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. *HDU1251 字典树

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

随机推荐

  1. C++逗号运算符与逗号表达式

    C++将赋值表达式作为表达式的一种,使赋值操作不仅可以出现在赋值语句中,而且可以以表达式形式出现在其他语句(如输出语句.循环语句等)中.这是C++语言灵活性的一种表现. 请注意,用cout语句输出一个 ...

  2. C++赋值运算符与赋值表达式

    赋值运算符 赋值符号“=”就是赋值运算符,它的作用是将一个数据赋给一个变量.如“a=3”的作用是执行一次赋值操作(或称赋值运算).把常量3赋给变量a.也可以将一个表达式的值赋给一个变量. 赋值过程中的 ...

  3. Android dialog 问题

    1.dialog.dismiss和dialog.cancel的区别 Cancel the dialog. This is essentially the same as calling dismiss ...

  4. mysql 监控 大批量的插入,删除,和修改

    监控大批量的插入,修改和删除: mysql> insert into aaa select * from aaa; mysql> SELECT trx_id, trx_state, trx ...

  5. Java基础:多态(重载和重写)

    转载请注明出处:jiq•钦's technical Blog (1)域与静态方法 记住"仅仅有普通方法的调用是多态的". 而域和静态方法不是:对于域的訪问.在编译期间就已经进行解析 ...

  6. [poj 1265]Area[Pick定理][三角剖分]

    题意: 给出机器人移动的向量, 计算包围区域的内部整点, 边上整点, 面积. 思路: 面积是用三角剖分, 边上整点与GCD有关, 内部整点套用Pick定理. S = I + E / 2 - 1 I 为 ...

  7. IE 下使用firebug

    javascript :var firebug=document.createElement('script');firebug.setAttribute('src','http://getfireb ...

  8. [置顶] 浅谈Android的资源编译过程

    Android APK 一.APK的结构以及生成 APK是Android Package的缩写,即Android application package文件或Android安装包.每个要安装到Andr ...

  9. MSSQL - 多表查询

    SELECT u.UserNumber, u.UserName, c.CarNumber, c.CarName, c.CarEngine, s.BuyLs, s.BuyTime FROM Tb_Sal ...

  10. Oracle Patch Bundle Update

    一.相关知识介绍 以前只知道有CPU(Critical Patch Update)和PSU(Patch Set Update),不知道还有个Bundle Patch,由于出现了TNS-12531的BU ...