map
一道ACM的题:http://poj.grids.cn/problem/2804/
字典:
Time Limit:3000ms Memory limit: 65536kB
题目描述
你旅游到了一个国外的城市。那里的人们说的外国语言你不能理解。不过幸运的是,你有一本词典可以帮助你。
输入
首先输入一个词典,词典中包含不超过100000个词条,每个词条占据一行。每一个词条包括一个英文单词和一个外语单词,两个单词之间用一个空格隔开。而且在词典中不会有某个外语单词出现超过两次。词典之后是一个空行,然后给出一个由外语单词组成的文档,文档不超过100000行,而且每行只包括一个外语单词。输入中出现单词只包括小写字母,而且长度不会超过10。
输出
在输出中,你需要把输入文档翻译成英文,每行输出一个英文单词。如果某个外语单词不在词典中,就把这个单词翻译成“eh”。
样例输入
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay
atcay
ittenkay
oopslay
样例输出
cat
eh
loops
提示
输入比较大,推荐使用C语言的I / O函数。
我用map做,超时了,搞了很久,都没搞清楚!别人也用map做没超时!不知什么原因?
我的代码:
#include<iostream>
#include<string>
#include<map>
using namespace std;
char ch[25];
char dic[12];
char fdic[12];
int main()
{
char search[12];
char out[12];
int temp;
int i,j;
map<string,string> m;
temp=0;
while(gets(ch))
{
if(ch[0]==0)
break;
sscanf(ch,"%s %s",dic,fdic);
temp++;
m[fdic]=dic;
}
while(scanf("%s",search)!=EOF)
{
map<string,string>::iterator it;
it=m.find(search);
if(it!=m.end())
{
printf("%s\n",(it->second).c_str());
//printf("%s",it->second<<endl;
//cout<<it->second<<endl;
}
else
{
printf("eh\n");
}
}
网上AC的代码:
#include<map>
#include<string.h>
#include<stdio.h>
using namespace std;
struct str{
bool operator<(const str &a) const
{
return strcmp(e,a.e)<0;
//e[0]=0;
}
char e[12];
};
int main()
{
map<str,str> d;
str word,s;
char t[30];
while(gets(t)&&t[0]!=0)
{
sscanf(t,"%s %s",s.e,word.e);
d[word]=s;
}
while(scanf("%s",word.e)!=EOF)
{
map<str,str>::iterator it=d.find(word);
if(it!=d.end())
printf("%s\n",it->second.e);
else
printf("eh\n");
}
return 0;
}