What Are You Talking About hdu1075

来源:互联网 发布:wamp数据库账号密码 编辑:程序博客网 时间:2024/05/21 06:58

What Are You Talking About

Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 102400/204800K (Java/Other)
Total Submission(s) : 8   Accepted Submission(s) : 3
Problem Description
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
STARTfrom fiwohello difhmars riwosfearth fnnvklike fiiwjENDSTARTdifh, i'm fiwo riwosf.i fiiwj fnnvk!END
 

Sample Output
hello, i'm from mars.i like earth![hint]Huge input, scanf is recommended.[/hint]
字符串类型的题目,但是在操作的时候需要注意的是操作的时候注意字典的存储方式和查找方式,以及对需要翻译的串的存储单独的单词的操作。总体本题包含了字符串的处理,以及查找的时间复杂度的问题。根据本题的信息,如果用数组存储字典的话,在查找的时候时间复杂度为
O(n),而且是对单独的一个单词的操作,一共包括多余300个的单词,以及其他的字符,如果字典存储的数量非常的大的时候是很费时间的,所以要在查找的算法上进行优化,可以用键值对的形式map,以下代码实现包括两种,数组实现(超时),map实现AC,
//数组实现#include <cstdio>#include <iostream>#include <algorithm>#include <map>#include <cstring>using namespace std;#define maxx 20#define maxn 3100typedef struct node {char a[maxx];char b[maxx];}Node;Node s[25000000];void match(char str[],int n){int len = strlen(str);char f[maxx];int k=0;for(int i=0;i<len;i++){if(str[i]>='a'&&str[i]<='z')f[k++]=str[i];else//如果当前字符不是字母 {if(k>0)//如果单词存在 {f[k]='\0';//cout<<"  ----- "<<f<<endl;int flag = 0;for(int g=0;g<n;g++)//判断是否存在于字典当中 {//cout<<"--"<<s[g].b<<"--"<<endl;if(strcmp(f,s[g].b)==0){printf("%s",s[g].a);flag = 1;break;}}if(flag==0)printf("%s",f);}printf("%c",str[i]);//输出当前非字母元素 k=0;//还原f数组的下标为0 }}if(k>0)//如果最后还有匹配的单词 {f[k]='\0';int flag = 0;for(int g=0;g<n;g++)//判断是否存在于字典当中 {if(strcmp(f,s[g].b)==0){printf("%s",s[g].a);flag = 1;break;}}if(flag==0)printf("%s",f); }  printf("\n");}void index(){char sta[maxn];gets(sta);int k=0;while(1){gets(sta);if(strcmp(sta,"END")==0)break;sscanf(sta,"%s %s",s[k].a,s[k].b);//cout<<s[k].a<<" ---"<<s[k++].b<<endl;k++;}/*for(int i=0;i<k;i++){cout<<"--"<<s[i].a<<"--"<<s[i].b<<endl;}*/gets(sta);while(1){gets(sta);if(strcmp(sta,"END")==0)break;match(sta,k);}}int main(){index();return 0; } 

//map实现#include <cstdio>#include <iostream>#include <algorithm>#include <map>#include <cstring>using namespace std;#define maxx 20#define maxn 4000map<string,string> mapp;void match(char str[]){int len = strlen(str);char f[maxx];char s[maxx];int k=0;map<string,string>::iterator iter; for(int i=0;i<len;i++){if(str[i]>='a'&&str[i]<='z')f[k++]=str[i];else//如果当前字符不是字母 {if(k>0)//如果单词存在 {f[k]='\0';//cout<<"  ----- "<<f<<endl;iter = mapp.find(f);if(iter!=mapp.end())cout<<iter->second;elseprintf("%s",f);}printf("%c",str[i]);//输出当前非字母元素 k=0;//还原f数组的下标为0 }}if(k>0)//如果最后还有匹配的单词 {f[k]='\0';iter = mapp.find(f);<span style="white-space:pre"></span>//map的find函数的O(lgn)if(iter!=mapp.end())<span style="white-space:pre"></span>//查找到的时候输出翻译之后的串cout<<iter->second;elseprintf("%s",f);否则输出原字符串。 }  printf("\n");}void index(){char sta[maxn];char a[maxx],b[maxx];gets(sta);while(1){gets(sta);if(strcmp(sta,"END")==0)break;sscanf(sta,"%s %s",a,b);<span style="white-space:pre"></span>//对字典串进行处理//cout<<s[k].a<<" ---"<<s[k++].b<<endl;mapp[b]=a;<span style="white-space:pre"></span>//map可以类似数字一样的存储值。}/*for(int i=0;i<k;i++){cout<<"--"<<s[i].a<<"--"<<s[i].b<<endl;}*/gets(sta);while(1){gets(sta);if(strcmp(sta,"END")==0)break;match(sta);}}int main(){index();return 0; } 


0 0
原创粉丝点击