HDOJ 1075 What are you talking about ? 字典树
来源:互联网 发布:网络公益联盟 编辑:程序博客网 时间:2024/05/31 06:21
What Are You Talking About
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K (Java/Others)Total Submission(s): 17913 Accepted Submission(s): 5826
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
STARTfrom fiwohello difhmars riwosfearth fnnvklike fiiwjENDSTARTdifh, i'm fiwo riwosf.i fiiwj fnnvk!END
Sample Output
hello, i'm from mars.i like earth!HintHuge input, scanf is recommended.
#include<stdio.h>#include<iostream>#include<string>#include<malloc.h>#include<cstring>using namespace std;//字符数#define MAX 26//内存最大限制#define MEMORYLIMIT 100000*1024const char CH='a';struct Node{ Node * Next[MAX]; char * dic;};//先分配好内存,malloc比较浪费时间Node Memory[MEMORYLIMIT/sizeof(Node)];struct Trie{ Node * root; int allocp;//当前在分配内存中的位置 Trie(){ init(); } void init(){ allocp=0; memset(Memory,0,sizeof(Memory)); root=getNode(); } //从分配内存中获得一个节点 Node * getNode() { Node * tmp=&Memory[allocp++]; tmp->dic=NULL; for(int i=0;i<MAX;i++) tmp->Next[i]=NULL; return tmp; } void insert(char *str,char *dic) { Node *tmp=root; for(int i=0;str[i];i++) { int id=str[i]-CH; if(tmp->Next[id]) ; else tmp->Next[id]=getNode(); tmp=tmp->Next[id]; } tmp->dic=dic; } char *query(char *str) { if(root==NULL) return 0; Node *tmp=root; for(int i=0;str[i];i++) { int id=str[i]-CH; if(tmp->Next[id]) tmp=tmp->Next[id]; else return NULL; } return tmp->dic; }};int main(){ int n,m,cnt=0; char s[30008],word[12],str[11]; Trie trie; gets(s); while(scanf("%s",s),s[0]!='E') { scanf("%s",str); char *x=(char*)malloc(sizeof(char)*11); strcpy(x,s); trie.insert(str,x); } gets(s); gets(s); while(gets(s),s[0]!='E') { int k=0; for(int i=0;s[i];i++) { if(s[i]<'a'||s[i]>'z'){ word[k++]='\0'; if(k!=1) { char *t=trie.query(word); if(t)printf("%s",t); else printf("%s",word); } k=0; printf("%c",s[i]); } else word[k++]=s[i]; } printf("\n"); } return 0;}
0 0
- HDOJ--1075--What Are You Talking About【字典树】
- HDOJ 1075 What Are You Talking About(字典树)
- HDOJ 1075 What are you talking about ? 字典树
- HDOJ 1075 What Are You Talking About (字典树映射)
- HDOJ 1075 What Are You Talking About
- HDOJ What Are You Talking About 1075
- HDOJ 1075 What Are You Talking About
- HDOJ 题目1075What Are You Talking About(字典树)
- HDU 1075 What Are You Talking About(字典树)
- hdu 1075 What Are You Talking About (字典树)
- hdu-1075-What Are You Talking About-(字典树)
- hdu 1075 What Are You Talking About(字典树方法)
- hdu 1075 What Are You Talking About 字典树
- HDU 1075 What Are You Talking About 字典树
- hdu 1075 What Are You Talking About(字典树)
- hdu 1075 What Are You Talking About(字典树)
- hdu 1075 What Are You Talking About (字典树)
- hdu 1075 What Are You Talking About 字典树
- 笔试面试最常涉及到的12种排序算法(包括插入排序、二分插入排序、希尔排序、选择排序、冒泡排序、鸡尾酒排序、快速排序、堆排序、归并排序、桶排序、计数排序和基数排序)进行了详解。每一种算法都有基本介绍、算
- Codeforces 594A - Warrior and Archer (思维)
- ps快捷键对应的英文字母缩写
- Java中 对象克隆实例
- [NOIP2015]推销员
- HDOJ 1075 What are you talking about ? 字典树
- 2015年大二上-数据结构-栈(5)- 后缀表达式
- STS怎么创建一个springMVC的Maven项目
- linux 修改 机器名 root@localhost
- Hibernate检索方式的学习2015-11-14
- Understanding glibc malloc
- VM虚拟机快速安装LAMP后的配置
- Bochs调试指令
- 数据仓库与数据库的区别