Language of FatMouse(trie树)

G – Language of FatMouse
Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu
Submit 
Status 
Practice 
ZOJ 1109 Appoint description:  System Crawler  (2015-10-12)

Description

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
zoj这题真恶心,竟然卡内存,只能开到2倍,大一点都超内存。
AC代码:
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<map>
using namespace std;
#define T 100005
#define inf 0x3f3f3f3f
#define CRL(a) memset(a,0,sizeof(a))
typedef long long ll;
struct trie
{
	string str;
	int kid[26];
	trie()
	{
		CRL(kid);
	}
}tr[T<<1];
char s[15],ss[15],str[25];
int c=0;
void insert()
{
	int x=0,d,i;
	for(i=0;ss[i];++i){
		d = ss[i]-'a';
		if(tr[x].kid[d]==0){
			tr[x].kid[d]=++c;
			x=c;
		}
		else
			x=tr[x].kid[d];
	}
	tr[x].str=s;
}
string find()
{
	int i,d,x=0;
	for(i=0;s[i];++i){
		d = s[i]-'a';
		if(tr[x].kid[d]==0)
			return "";
		x=tr[x].kid[d];
	}
	return tr[x].str;
}
int main()
{
	/*freopen("input.txt","r",stdin);*/
   
	while(cin.getline(str,25),strcmp(str,"\0"))
	{ 
		int f=0;
       sscanf(str, "%s %s", s, ss);
		insert();
	}
	while(cin>>s)
	{
		string t=find();
		if(t=="\0"){
			cout <<"eh\n";
		}
		else
		{
			cout << t<< endl;
		}
	}
	return 0;
}
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<map>
using namespace std;
#define T 100005
#define inf 0x3f3f3f3f
#define CRL(a) memset(a,0,sizeof(a))
typedef long long ll;
struct trie
{
	string str;
	int kid[26];
	trie()
	{
		CRL(kid);
	}
}tr[T<<2];
string s,ss,str;
int c=0;
void insert()
{
	int x=0,d,i;
	for(i=0;ss[i];++i){
		d = ss[i]-'a';
		if(tr[x].kid[d]==0){
			tr[x].kid[d]=++c;
			x=c;
		}
		else
			x=tr[x].kid[d];
	}
	tr[x].str=s;
}
string find()
{
	int i,d,x=0;
	for(i=0;s[i];++i){
		d = s[i]-'a';
		if(tr[x].kid[d]==0)
			return "";
		x=tr[x].kid[d];
	}
	return tr[x].str;
}
int main()
{
	/*freopen("input.txt","r",stdin);*/
   
	while(getline(cin,str),str!="\0")
	{ 
		int f=0;s="",ss="";
		for(int i=0;str[i];++i){
			if(str[i]==' '){
				f=1;
			}
			else if(f)
			{
				ss+=str[i];
			}
			else
			{
				s+=str[i];
			}
		}
		insert();
	}
	while(cin>>s)
	{
		string t=find();
		if(t=="\0"){
			cout <<"eh\n";
		}
		else
		{
			cout << t<< endl;
		}
	}
	return 0;
}
    原文作者:Trie树
    原文地址: https://blog.csdn.net/zsc2014030403015/article/details/49189391
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞