poj 2503 Trie模板

Babelfish
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 43397   Accepted: 18335

Description

You have just moved from Waterloo to a big city. The people here speak an incomprehensible(费解的) dialect(方言) of a foreign language. Fortunately, you have a dictionary to help you understand them.

Input

Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase(小写字母) letters.

Output

Output is the message translated to English, one word per line. Foreign 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

Sample Output

cat
eh
loops

Hint

Huge input and output,scanf and printf are recommended.


题意:前面一组数据是英问和火星文对应的译码表  接着输入的是火星文,要求根据译码表吧火星文翻译成英文,如果找不到则输出 eh

#include 
#include 
#include 
struct Node
{
	char low[11];//存放最终的字符串 
	Node *next[26];//26个英文字母指针 
};
Node *root,*p,*q;
void Init(Node *root)//初始化 
{
	root->low[0]='#';//'#'表示没有存储字符串 
	for(int i=0;i<26;i++)
		root->next[i]=NULL;
}
void BuildTire(char *s2,char *s1)
{
	int i,v;
	for(i=0,p=root;inext[v]==NULL)
		{
			q=(struct Node*)malloc(sizeof(Node));
			Init(q);
			p->next[v]=q;
		}
		p=p->next[v];
	}
	strcpy(p->low,s1);
}
Node *Insearch(char *str)
{
	int i,v;
	for(i=0,p=root;inext[v]==NULL)
			break;
		p=p->next[v];
	}
	return p;
}
int main()
{
	char str[23],s1[11],s2[11];
	root=(struct Node *)malloc(sizeof(Node));
	Init(root);
	while(gets(str) && str[0]!='\0')
	{
		sscanf(str,"%s%s",s1,s2);
		BuildTire(s2,s1);
	}
	while(gets(str) && str[0]!='\0')
	{
		Node *r=Insearch(str);
		if(r->low[0]!='#')
			printf("%s\n",p->low);
		else
			printf("eh\n");
	}
	return 0;
}

你可能感兴趣的:(字典树(Trie))