题目链接:ZOJ1109
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
题目分析:跟之前HDU1075火星文的题几本一样,我们只需要标记好串尾就行了。
//
// main.cpp
// ZOJ1109
//
// Created by teddywang on 16/4/1.
// Copyright © 2016年 teddywang. All rights reserved.
//
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef struct node {
int num;
node *next[26];
}trienode;
trienode *root;
char s[100040][13];
int l;
void insert_node(char *t,int num)
{
int len=strlen(t);
trienode *h,*r=root;
for(int i=0;i<len;i++)
{
int buf=t[i]-'a';
if(r->next[buf]==NULL)
{
h=new node;
h->num=-1;
for(int j=0;j<26;j++)
h->next[j]=NULL;
r->next[buf]=h;
r=h;
}
else r=r->next[buf];
}
r->num=num;
}
int find(char *t)
{
int len=strlen(t);
trienode *r=root;
for(int i=0;i<len;i++)
{
int buf=t[i]-'a';
if(r->next[buf]==NULL)
return -1;
else
r=r->next[buf];
}
return r->num;
}
void del(trienode *r)
{
for(int i=0;i<26;i++)
{
if(r->next[i]!=NULL)
del(r->next[i]);
}
free(r);
}
int main()
{
root=new node;
root->num=-1;
for(int i=0;i<26;i++)
root->next[i]=NULL;
l=0;
char t[30];
while(gets(t)&&t[0]!='\0')
{
int i=0;
for(i=0;t[i]!=' ';i++)
{
s[l][i]=t[i];
}
insert_node(t+i+1,l);
l++;
memset(t,0,sizeof(t));
}
while(~scanf("%s",t))
{
int pos=find(t);
if(pos==-1)
printf("eh\n");
else
printf("%s\n",s[pos]);
}
del(root);
}