[HDOJ1075]What Are You Talking About
2015-08-26 11:26
405 查看
What Are You Talking About
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K (Java/Others)Total Submission(s): 17546 Accepted Submission(s): 5712
[align=left]Problem Description[/align]
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?
[align=left]Input[/align]
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.
[align=left]Output[/align]
In this problem, you have to output the translation of the history book.
[align=left]Sample Input[/align]
START
from fiwo
hello difh
mars riwosf
earth fnnvk
like fiiwj
END
START
difh, i'm fiwo riwosf.
i fiiwj fnnvk!
END
[align=left]Sample Output[/align]
hello, i'm from mars.
i like earth!
Hint
Huge input, scanf is recommended.
字典树可以过,不过这个题用STL更明显一些。
注意cout会超时,printf的话会报一个warnin,如下:
warning: cannot pass objects of non-POD type `struct std::basic_string<char, std::char_traits<char>, std::allocator<char> >' through `...'; call will abort at runtime
结果就是导致程序无法正常运行。
我采用取消cin cout与stdin的同步来解决cout会超时的问题:
ios::sync_with_stdio(false);
代码如下:
#include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <iostream> #include <cmath> #include <queue> #include <map> #include <stack> #include <list> #include <vector> #include <cctype> using namespace std; typedef map<string, string> mss; typedef pair<string, string> pss; mss dic; pss tmp; char a[12]; char b[12]; char buf[12]; int main() { ios::sync_with_stdio(false); // freopen("in", "r", stdin); gets(buf); //START while(scanf("%s %s", a, b) && strcmp(a, "END")) { //dic tmp = make_pair(b, a); dic.insert(tmp); } getchar(); char ch; int cnt = 0; while(~scanf("%c", &ch)) { if(isalpha(ch)) { buf[cnt++] = ch; } else { buf[cnt] = 0; cnt = 0; if(strcmp(buf, "END") == 0) { break; } if(dic.find(buf) != dic.end()) { cout << dic[buf]; } else { cout << buf; } cout << ch; } } return 0; }
相关文章推荐
- 用户空间与内核空间,进程上下文与中断上下文
- android 点击按钮返回上一层
- TCPdump抓包命令详解2
- POJ 3264 Balanced Lineup(RMQ)
- iOS-tabbar控制从首页内容跳转到第二个Controller
- UE4 Shadow Casting
- HDOJ 1028 Ignatius and the Princess III(DP)
- 【Leetcode】Median of Two Sorted Arrays
- [iOS]时间筛选之<本周、上周、更早>
- 二叉树的建立,以及非递归遍历
- Android中的Shape使用总结
- 安装 vCenter Server Appliance 修补程序
- Linux shell 字符串常用操作
- bootstrap2和bootstrap3的用法区别概述(三)
- 一个23岁大学生的Raspberry Pi开源项目
- sql扫盲(2)
- UE4 Lightmass Global Illumination
- Volley﹕ [37470] NetworkDispatcher.run: Unhandled exception java.lang.NullPointerException
- Struts2,Hibernate和Spring之间的框架整合关系
- YII的关联查询