字典树--What Are You Talking About

What Are You Talking About

Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book into English. Can you help him? 

Input

The problem has only one test case, the test case consists of two parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string "START", this string should be ignored, then some lines follow, each line contains two strings, the first one is a word in English, the second one is the corresponding word in Martian's language. A line with a single string "END" indicates the end of the directory part, and this string should be ignored. The book part starts with a single line contains a string "START", this string should be ignored, then an article written in Martian's language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should translate it and write the new word into your translation, if you can't find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. Space(' '), tab('\t'), enter('\n') and all the punctuation should not be translated. A line with a single string "END" indicates the end of the book part, and that's also the end of the input. All the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters. 

Output

In this problem, you have to output the translation of the history book. 

Sample Input

START
from fiwo
hello difh
mars riwosf
earth fnnvk
like fiiwj
END
START
difh, i'm fiwo riwosf.
i fiiwj fnnvk!
END

Sample Output

hello, i'm from mars.
i like earth!

        
  

Hint

 

Huge input, scanf is recommended.

        


 用STL方法:

#include <iostream>
#include <map>
#include <stdio.h>
#include <string.h>
using namespace std;

int main(){
	string a,b;
	map<string,string> mp;//键值对mp 
  	cin>>a;//输入START 
  	while(cin>>a&&a!="END"){
  		cin>>b;
		mp[b]=a;//mp[Martian's language] = English 
	}
  	cin>>a;//输入END 
  	getchar(); //吸收回车 
  	char ch[3005];
  
  	while(1){
		gets(ch);
		int i,len;
	    if(strcmp(ch,"END")==0)
			break;
		len=strlen(ch);
		b="";
		for(i=0;i<len;++i){
			if(ch[i]<'a'||ch[i]>'z'){
		    	//输出字母或单词 
				if(mp[b]!="")//mp[b] != null
					cout<<mp[b];//输出English 
		       else//mp[b] = null
			     	cout<<b;//直接输出 
		        
				b="";//初始化 
			    cout<<ch[i];//输出该字符ch[i](符号) 
			}
			else//如果输入的为字母,这加入到b字符串中 
				b+=ch[i];
		}
		cout<<endl;//换行 
  	}
  	return 0;
}

用字典树方法:

#include<stdio.h>
#include<iostream>
#include<string.h>
#define MAX 1000+5 
using namespace std;

char s[MAX],s1[MAX],s2[MAX];//作用下面有说 

struct node{
	int count;//count用于标记是否是一个单词 
	char s[200];//用于记录对应的English单词 
	struct node *a[27];
	node(){
        count=0;
        memset(a,0,sizeof(a));
	}
}*root,*d;


void insert(char c1[],char c2[]){//C1-->Martian's language C2-->English
	d=root;//从根节点开始插入 
	int w=strlen(c1);
	for(int i=0;i<w;i++){ //建树核心代码
	    if(d->a[c1[i]-'a']==NULL)//如果该字母节点还未建立 
	        d->a[c1[i]-'a'] = new node; //则建立该节点 
	    d=d->a[c1[i]-'a']; //移动到该节点 
	}
	d->count=1;//标记该单词结束位置 
	strcpy(d->s,c2);//将该English单词加入 
}

char *find(char c1[]){//C1-->Martian's language
	d=root;//从根节点开始查找 
	int w=strlen(c1);
	for(int i=0;i<w;i++){
	    d=d->a[c1[i]-'a'];
	    if(d==0)//该不存在 
			return NULL;
	}
	if(d->count==0)//输入的单词并不是一个完整单词 
		return NULL;
	return d->s;//满足则返回English单词 
}

int main(){
	root = new node;//建立根节点 
	
	scanf("%s",&s);//吸收START 
	while(1){
		scanf("%s",&s1); //s1 --> English
		if(strcmp(s1,"END")==0)	break; //END表示退出 
		scanf("%s",&s2);//s2 --> Martian's language
		insert(s2,s1);//建立字典树 
	}
	scanf("%s",&s);//吸收END 
	getchar();//吸收回车 
	
	while(1){
		gets(s);//吸收START 
		if(strcmp(s,"END")==0)	break; //判断是否结束 
		for(int i=0;i<strlen(s);i++){
			int l=0;//索引
			char temp[MAX<<2];//用于记录一个单词
			 
		    while(s[i]>='a'&&s[i]<='z')//将一个单词中的字母合并起来 
		        temp[l++]=s[i++];
		    temp[l]='\0';//字符串最后有个'\0' 
		    char *ans=find(temp);//查找 
		    
			if(ans==NULL)//不存在的字母就原封不动输出 
				cout<<temp;
		    else //否则就输出English单词 
				cout<<ans;
		    cout<<s[i];//符号输出
		}
		cout<<endl;//回车 
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章