博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
What Are You Talking About(字典树)
阅读量:5020 次
发布时间:2019-06-12

本文共 5052 字,大约阅读时间需要 16 分钟。

What Are You Talking About

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)

Total Submission(s): 7547    Accepted Submission(s): 2335

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.
 

 

Author
Ignatius.L
 

这道题的题意有点不清不楚,以例子中存在于字典的一个词“fiwo”为例,当fiwo后为字母如“fiwoff”、“fiwoaf”等等时,fiwo就不翻译(因为不是独立一个单词),只要fiwo后不是字母,比如“fiwo,”、“fiwo’ ”、“fiwo   ”等等,fiwo都要翻译。

所以翻译时既要判断fiwo是否在字典树中,又要判断fiwo后面的是字母还是非字母。

AC CODE:

 

1 #include 
2 #include
3 #include
4 #include
5 using namespace std; 6 7 struct Trie 8 { 9 char word[11]; 10 struct Trie *next[26]; 11 Trie() 12 { 13 for(int i = 0; i < 26; i++) 14 next[i] = NULL; 15 word[0] = '\0'; 16 } 17 }; 18 19 int main() 20 { 21 char s[11], dic[11], t[3002], c; 22 struct Trie *root = new Trie, *temp; 23 scanf("%s%*c", s); //START 24 //构造字典树,储存字典上的单词 25 while(scanf("%s", s)) 26 { 27 if(s[0] == 'E') break; 28 else 29 { 30 scanf("%s", dic); 31 temp = root; 32 for(int k = 0; dic[k] != '\0'; k++) 33 { 34 if(temp ->next[dic[k]-'a'] != NULL) 35 temp = temp ->next[dic[k]-'a']; 36 else 37 { 38 temp ->next[dic[k]-'a'] = new Trie; 39 temp = temp ->next[dic[k]-'a']; 40 } 41 } 42 strcpy(temp ->word, s); 43 } 44 } 45 scanf("%s%*c", s); //START 46 while(scanf("%c", &c) && c != 'E') //先输入每一行的首字母看是否为E,不是才继续输入 47 { 48 //输入每一行进行翻译 49 int k = 0; 50 t[k++] = c; 51 while(scanf("%c", &c) && c != '\n') //输入到行末为止 52 { 53 t[k++] = c; 54 } 55 t[k] = '\n'; 56 t[k+1] = '\0'; 57 //翻译 58 string str = ""; 59 temp = root; 60 for(int j = 0; j <= k; j++) 61 { 62 str += t[j]; 63 //字母 64 if(t[j] >= 'a' && t[j] <= 'z') 65 { 66 if(temp ->next[t[j]-'a'] != NULL) 67 { 68 temp = temp ->next[t[j]-'a']; 69 } 70 else 71 { 72 for(j++; t[j] <= 'z' && t[j] >= 'a'; j++) 73 { 74 str += t[j]; 75 } 76 str += t[j]; 77 cout << str; 78 str.clear(); 79 temp = root; 80 } 81 } 82 //temp ->word[0] != '\0',则t[j](已确定不是字母)之前就是字典中所存在的某一单词。 83 //比如第一个例子中的“difh,”中的“,”就是此类 84 else if(temp ->word[0] != '\0') 85 { 86 printf("%s", temp ->word); 87 str.clear(); 88 j--; 89 temp = root; 90 } 91 else 92 { 93 cout << str; 94 str.clear(); 95 temp = root; 96 } 97 }//search for 98 } 99 return 0;100 }

 

 

 

转载于:https://www.cnblogs.com/cszlg/archive/2012/08/15/2910560.html

你可能感兴趣的文章
关于根据Build Platform或者OS 加载x86或者x64 dll的问题
查看>>
程序员高效开发的几个技巧
查看>>
js-权威指南学习笔记19.2
查看>>
hexo 搭建博客
查看>>
关于 UIWebView 几个高级用法
查看>>
maven创建的项目中无法创建src/main/java 解决方案
查看>>
华为软件开发云测评报告二:代码检查
查看>>
集合1
查看>>
js 原生 ajax
查看>>
关键词 virtual
查看>>
建造者模式(屌丝专用)
查看>>
UVALive 4730 Kingdom +段树和支票托收
查看>>
[APIO2010]特别行动队
查看>>
[SCOI2016]幸运数字
查看>>
SpringBoot 集成ehcache
查看>>
初步swift语言学习笔记2(可选类型?和隐式可选类型!)
查看>>
Nginx + Tomcat 反向代理 如何在高效的在一台服务器部署多个站点
查看>>
在Vs2012 中使用SQL Server 2012 Express LocalDB打开Sqlserver2012数据库
查看>>
在Macos下完美解决Adobe Dreamweaver CC 2018 汉化及操作方法
查看>>
【转】 Newtonsoft.Json高级用法
查看>>