HDOJ--1075--What Are You Talking About【字典树】
来源:互联网 发布:淘宝怎么才能做起来 编辑:程序博客网 时间:2024/05/09 05:46
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.
Author
Ignatius.L
字典树的插入和查找,我的代码有些单词会被前缀影响,有些不会,不知道改哪,在网上找了一个代码,发现自己没有将字典树的后缀初始化为空集,所以后来再判断空集时出现错误,感觉略坑
#include<cstdio>#include<iostream>#include<cmath>#include<cstring>#include<string.h>#include<ctype.h>#include<stack>#include<queue>#include<set>#include<algorithm>#include <limits.h>#define PI acos(-1)#define MAX 26using namespace std;char str[40000];typedef struct Trie{ Trie *next[MAX]; char s[20]; Trie() { s[0]=0; for (int i=0;i<=26;i++) next[i]=NULL; }};Trie root;void addTrie(char *str2,char str1[]){ int len = strlen(str2); Trie *p = &root, *q; for(int i=0; i<len; ++i) { int id = str2[i]-'a'; if(p->next[id] == NULL) { q = new Trie; //for(int j=0; j<MAX; ++j) // q->next[j] = NULL; p->next[id] = q; p = p->next[id]; } else p=p->next[id]; } strcpy(p->s,str1);}Trie* findTrie(int l,int r){ Trie *p = &root; for(int i=l; i<=r; ++i) { int id = str[i]-'a'; if(p->next[id] == NULL) //若为空集,表示不存以此为前缀的串 return 0; p = p->next[id]; } if (strcmp(p->s,"")) return p; return 0;}int main(){ char str1[20],str2[20],c; scanf("%s",str1); while(scanf("%s",str1),strcmp(str1,"END")) { scanf("%s",str2); addTrie(str2,str1); } scanf("%s",str1); c=getchar(); while(gets(str),strcmp(str,"END")) { int len=strlen(str); str[len+1]=0; for (int i=0;i<len;i++) { if(isalpha(str[i])) { int j=i; while (isalpha(str[j+1])) j++; Trie *p=findTrie(i,j); if (p) printf("%s",p->s); else { for (int k=i;k<=j;k++) printf("%c",str[k]); } i=j; } else printf("%c",str[i]); } printf("\n"); } return 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 字典树
- hdu 1003 Max Sum
- Hadoop format的时候The directory is already locked
- Unity控制角色移动错误问题集合、第一人称视角
- org.apache.hadoop.hdfs.server.datanode.DataNode: java.io.IOException: Incompatible namespaceIDs
- Re-installation failed due to different application signatures.
- HDOJ--1075--What Are You Talking About【字典树】
- POJ 2104 —— 平方分割 || 归并树 || 划分树(未完)
- subclipse
- uva 108 Maximum Sum 最大子矩阵和
- FFMPEG详解
- 深入浅出FFMPEG(二) SDK version 0.11
- (转)ffmpeg 解码ts流示例 (2012-12-25 18:00:32)
- 深入浅出FFMPEG(一) SDK version 0.1
- qtp automation framework 1