ZJU 1109 解题报告

Language of FatMouse

Time Limit: 10 Seconds Memory Limit: 32768 KB

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

赤裸裸的字典树,用map也可以做.两者再时间和空间上的效率不一样.用字典树做的时间140ms,空间17084KB.用map做时间较慢用去470ms,但空间的优势非常明显只有9560KB.两份代码如下:

CODE 1

//map
//真的是短小精悍
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<algorithm>
#include<map>
 
using namespace std;
 
int main()
{
  int i,j,k;
  char s[500],s1[100],s2[100];
  string ss1,ss2,ss;
  map <string,string> dic;
  map <string,string> ::iterator it;
  while(gets(s))
    {
      ss=s;
      if(ss=="") break;
      sscanf(s,"%s %s",s1,s2);
      ss1=s1;ss2=s2;
      dic[ss2]=ss1;
    }
  while(gets(s))
    {
      ss1=s;
      it=dic.find(ss1);
      printf("%sn",it==dic.end()?"eh":it->second.c_str());
    }
  return 0;
}

CODE 2

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<string>
using namespace std;
 
struct trie
{
  int i;
  string s;
  trie *node[26];
  trie()
    {
      for(i=0;i<26;i++)
        node[i]=NULL;
    }
} *root;
 
void insert(char *s2,char *s1)
{
  int i,c;
  trie *p=root;
  for(i=0;s1[i];++i)
    {
      c=s1[i]-'a';
      if(p->node[c]==NULL) p->node[c]=new trie();
      p=p->node[c];
    }
  p->s=s2;
}
 
void query(char *s)
{
  trie *p=root;
  int i,c;
  for(i=0;s[i];++i)
    {
      c=s[i]-'a';
      if(p->node[c]==NULL)
        {
          printf("ehn");
          return;
        }
      p=p->node[c];
    }
  printf("%sn",p->s.c_str());
}
 
int main()
{
  int i,j,k;
  char s[30];
  root=new trie();
  while(gets(s))
    {
      if(strlen(s)==0) break;
      i=0;
      while(s[i]!=' ') ++i;
      s[i]='\0';
      insert(s,s+i+1);
    }
  while(scanf("%s",s)!=EOF) query(s);
  return 0;
}
» 本博客采用署名 2.5 中国大陆许可协议进行许可,本文版权归作者所有,欢迎转载,但必须在明显位置给出原文连接。
anyShare分享到:

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>