POJ 2503 Babelfish(字典树)

Babelfish

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 35009   Accepted: 14979

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

纯字典树水题。

学到的知识:

① sscanf 函数,scanf函数的兄弟,把字符串按指定格式读入到指定变量。

② Node 节点置空,可以不用NULL,而是用0更简洁安全。

/*
Trie(字典树)
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
typedef __int64 ll;

typedef struct Node{
	char word[15];
	struct Node *ch[27];	//没想到ch[]的初值可以为0,一开始我用NULL异常退出了
}node;

node *T;
int index;

void add(char s[],char wd[]){
	node *u;
	u=T;
	int len=strlen(s),i;

	for(i=0;i<len;i++){
		if(u->ch[s[i]-'a']==0){
			node *t=(node*)malloc(sizeof(node));
			for(int j=0;j<27;j++)
				t->ch[j]=0;
			u->ch[s[i]-'a']=t;
		}
		u=u->ch[s[i]-'a'];
	}
	strcpy(u->word,wd);
}

void ask(char s[]){
	node* u=T;
	int i,len=strlen(s);
	for(i=0;i<len;i++){
		if(u==0){
			printf("eh\n");
			return ;
		}
		else
			u=u->ch[s[i]-'a'];
	}
	printf("%s\n",u->word);
}

int main()
{
	int i,k,j;
	char s1[20],s2[20],str[50];
	T=(node*)malloc(sizeof(node));
	for(i=0;i<27;i++)
		T->ch[i]=0;
	while(gets(str)){
		if(str[0]=='\0') break;
		sscanf(str,"%s %s",s1,s2);		//需要借助sscanf来处理
		add(s2,s1);
	}
	while(scanf("%s",s1)!=EOF){
		ask(s1);
	}
	return 0;
}
时间: 2024-10-11 21:03:24

POJ 2503 Babelfish(字典树)的相关文章

poj 2503 Babelfish(字典树或着STL)

Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 35828   Accepted: 15320 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

POJ 2503 Babelfish (Trie树 或 map)

Babelfish Time Limit: 3000MS        Memory Limit: 65536K Total Submissions: 34278        Accepted: 14706 Description You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately

poj 2503:Babelfish(字典树,经典题,字典翻译)

Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30816   Accepted: 13283 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

poj 2503 Babelfish (map,trie 树)

链接:poj 2503 题意:输入 语言A及翻译为语言B的词典,之后再输入语言B的单词,判断是否能从词典中找到, 若能找到,将其翻译为语言A,否则输出"eh". 思路:这题肯定得先将词典对应语言存起来,但是如果直接暴力找输入的单词是否出现过,必然会TLE 因为单词都是一对一的关系,可以用map实现 当然,trie树是用空间换时间,对于字符串的查找,在时间上有着相当的优势,因此也可以用trie树 注:sscanf函数,从一个字符串中读进与指定格式相符的数据. map实现:938MS #i

POJ 1451 T9 字典树+优先队列

题目来源:POJ 1451 T9 题意:给你一些单词 和优先值 然后当你按下数字的时候首先会出现哪个单词 就是我们平时手机的按键 思路:建一颗字典树 因为按一个数字对应多个字母 那么就有多种情况 我们要输出权值最大的一个 我用了优先队列 这里每个前缀的优先值是所有单词优先值的和 例如abc 5 abd 6 acd 7 那么a这个前缀的优先值是18 ab的优先值是11 #include <cstdio> #include <cstring> #include <queue>

POJ 2503 Babelfish(字典树)

题目链接:http://poj.org/problem?id=2503 题意:翻译单词,若在词典中找不到则输出eh. 思路:裸的字典树. 代码: #include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <algorithm> #include <string> #include <vector> using

poj 2503 Babelfish(Map、Hash、字典树)

题目链接:http://poj.org/bbs?problem_id=2503 思路分析: 题目数据数据量为10^5, 为查找问题,使用Hash或Map等查找树可以解决,也可以使用字典树查找. 代码(Map实现): #include <iostream> #include <sstream> #include <string> #include <map> using namespace std; int main() { char word[15], fo

北大ACM2503——Babelfish~~字典树

题目的意思是:给你几个字符串对str1,str2.输入完毕后有一个空行,然后是询问的输入,每行一个字符串,如果该字符串与str2相同,则输出str1,否则输出"eh". 这题字符串对达到100000,询问的也达到了100000个,所以,普通的方法必定超时.所以需要建立字典树. 这题还有一个比较麻烦的就是输入.如何控制那一个空行之后的询问输入,这是关键. 简单的字典树的应用. 下面是AC的代码: #include <iostream> #include <cstdio&

POJ 1451 T9 字典树

题意和手机的九键输入法一样.输入数据第一行给出有多少组测试数据,每组数据第一行给出w(0<=w<=1000),接下来w行给出一个单词以及该单词的出现频率p(1<=p<=100),每个单词的最大长度不超过100个字母:然后,给出一个整数m,接下来m行给出一个输入串,代表在手机上按了哪些键,每个输入串最多有100个字符,且以数字1作为结尾.要求根据给出输入串,输出在按这些键的过程中,输入法给出的首选项(即出现频率最高的单词),若没有对应输入的单词,则输出"MANUALLY&q