We all know that FatMouse doesn't speak English. But now he has to be prepared since our nation will join WTO soon. Thanks to Turing we have computers to help him.
Input Specification
Input consists of up to 100,005 dictionary entries, followed by a blank line, followed by a message of up to 100,005 words. Each dictionary entry is a line containing an English word, followed by a space and a FatMouse word. No FatMouse word appears more than once in the dictionary. The message is a sequence of words in the language of FatMouse, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.
Output Specification
Output is the message translated to English, one word per line. FatMouse words not in the dictionary should be translated as "eh".
Sample Input
dog ogday cat atcay pig igpay froot ootfray loops oopslay atcay ittenkay oopslay
Output for Sample Input
cat eh loops
Source: Zhejiang University Training Contest 2001
分析:
与poj2503是完全一样的题,但是poj上手动分配内存空间可以ac,而zoj则提示Memory Limit Exceeded
这里只想补几点知识:
(1)采用gets()输入比getline()快得多,但gets()不能从cin中读取数据,不易调试
gets函数的用法:
从stdin流中读取字符串,直到读取到换行符或EOF时停止,并将读取的结果存放在str指针所指向的字符数组中。换行符不作为读取串的内容,读取的换行符被转换为null值,并由此来结束字符串。
(2)sscanf与scanf类似,都是用来输入的,只是后者以屏幕(stdin)为输入源,前者以固定的字符串为输入源,默认以空格来区分开字符串
ac代码:
//与poj2503是完全一样的题,但是poj上手动分配内存空间可以ac,而zoj则提示Memory Limit Exceeded
#include <iostream>
#include<cstdio>
#include<map>
#include<string>
using namespace std;
//手动分配内存空间
/*char buf[50000000];
int sizet =0;
void* operator new(size_t size){
void *p = buf + sizet;
sizet += size;
return p;
}
void operator delete(void* p){
}
*/
//
int main()
{
char e[11],f[11],str[25];
map<string,string>t;
for(;;)
{
gets(str);
//cin>>str;
if(str[0]=='\0') break;//str[0]=='\n'无效,应该是gets和cin等的运行机制问题。若要保险,再加一句:str[0]=='\n'.
sscanf(str,"%s%s",e,f);//从str读取
t[f]=e;
}
while(scanf("%s",str)!=EOF)
{
string s=t[str];
if (s.length()==0)
printf("eh\n");
else
printf("%s\n",s.c_str());//转换为cstring类型的数据,用s.c_str()函数
}
return 0;
}
本文介绍了一道关于FatMouse语言翻译的编程题,涉及到输入输出、字符串处理等内容。文章提供了AC代码示例,并对比了使用不同输入函数的效果。
1万+

被折叠的 条评论
为什么被折叠?



