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.
 
 
 
 
题目意思:给你一本火星字典,然后根据火星字典,把一句话翻译成英文,如果一个词在火星字典里找不
          到就不翻译,直接写出来。
解题思路:通过字典树,将字典进行建树,然后查找,这个题的关键在于怎么把这段发翻译过来,于是想
          到通过函数或者ASCII码判断是否为小写字母,判断到不是时,就将前面的字母输入到树中进行
          查找,若找到就翻译,反之不翻译。这里找不到的判定标准有两个:1.遇到指针为空 2.指针不
          为空但最后一个字母不是树中标记过的字母,比如树中存在asdf,而你输入的为asd,那么就是
          找不到。
 
 
 
 
#include<iostream>
#include<cstring>
#include<algorithm>
#include<ctype.h>

using namespace std;

char s[3100];
char s2[20];
char s1[20];

struct trie{
	char v[15];  
	int mark;
	trie *next[26];
	trie(){
		mark=0;
		for(int i=0;i<26;i++)
		{
			next[i]=NULL;
		}
	}
}; 

trie *root;

void creat(trie *root,char *str,char *str1)
{
    int len=strlen(str);
	trie *p=root,*q;
	for(int i=0;i<len;i++)
	{
		int sign=str[i]-'a';
		if(p->next[sign]==NULL)
		{
			q=new trie();
			p->next[sign]=q;
			p=p->next[sign];
		}
		else
		{
			p=p->next[sign];			
		}
	}
	p->mark=-1;
	strcpy(p->v,str1);
}

bool find(trie *root,char *str,int m,int j)
{
	trie *p=root;
	int i=m;
	for(i;i<j;i++)
	{
			int id=str[i]-'a';
			p=p->next[id];
			if(p==NULL)
			{
				return false;
			}		
	}
	if(p->mark!=-1)
	    return false;
	cout<<p->v;
	return true;
}

void del(trie *root)
{
	for(int i=0;i<26;i++)
	{
		if(root->next[i])
		{
			del(root->next[i]); 
		}
	}
	delete(root);
}

int main()
{
	char str[10];
	root=new trie();
	scanf("%s",str);
	while(scanf("%s",s1)&&strcmp(s1,"END"))
	{
		scanf("%s",s2);
		creat(root,s2,s1);
	}
	scanf("%s",str);
	gets(s);
	while(gets(s)&&strcmp(s,"END")!=0)
	{
		int len=strlen(s);	
		int num=0;
		for(int a=0;a<len;a++)
		{
			if(s[a]>='a'&&s[a]<='z')
			{
				num++;
			}
			else
			{
				if(s[a-1]<'a'||s[a-1]>'z')
				{
					cout<<s[a];
					num=0;
				}
				else
				{
				    if(!find(root,s,a-num,a))
				    {
				    	for(int q=a-num;q<a;q++)
				    	{
				    		cout<<s[q];
						}
					}
				    cout<<s[a];
				    num=0;
				}
			}
		}
		if(!find(root,s,len-num,len))
	    {
	    	for(int q=len-num;q<len;q++)
			{
		    	cout<<s[q];
			}
		}
		cout<<endl;
	}	
	return 0;
}


 
 

 
 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值