字典樹--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;
}

 

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