hdu1075 What Are You Talking About(qsort二分查找& 字典树查找)

来源:互联网 发布:农业国家数据库查询 编辑:程序博客网 时间:2024/05/19 18:15

What Are You Talking About

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 15369    Accepted Submission(s): 4946


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. 
 
qsort 二分法:
#include <stdio.h>#include <cstring>#include <algorithm>#include <ctype.h>#include <stdlib.h>#define maxn 10000000using namespace std;struct word{char dic1[15];char dic2[15];};word dic[maxn];int cmp(const void *a,const void *b){word *A=(word*)a;word *B=(word*)b;return strcmp(A->dic2,B->dic2);}int binsearch(char *s,int k){int len=strlen(s);if(len==1)return -1;int head=0,tail=k-1;while(head<=tail){if(strcmp(dic[head].dic2,s)==0)return head;else if(strcmp(dic[tail].dic2,s)==0)return tail;else{int mid=(head+tail)/2;if(strcmp(dic[mid].dic2,s)==0)return mid;else if(strcmp(dic[mid].dic2,s)>0){tail=mid-1;}elsehead=mid+1;}}return -1;}int main(){char st[10];char test[3010];char test2[3010];gets(st);int o=0;while(gets(test)){if(strcmp(test,"END")==0)break;int len=strlen(test);int k=0;int sign=0;for(int i=0;i<len;i++){if(test[i]==' '){sign=1;k=0;continue;}if(!sign){dic[o].dic1[k++]=test[i];}else{dic[o].dic2[k++]=test[i];}}o++;}//sort(dic,dic+o,cmp);//不能给字符数组排序TVT;qsort(dic,o,sizeof(dic[0]),cmp);/* printf("test::\n");for(int i=0;i<o;i++){printf("%s %s\n",dic[i].dic1,dic[i].dic2);}printf("test::\n");  */gets(st);while(gets(test2)){if(strcmp(test2,"END")==0)break;int len=strlen(test2);char magic[20];memset(magic,'\0',sizeof(magic));int k=0;for(int i=0;i<len;i++){if(isalpha(test2[i]))magic[k++]=test2[i];else if(!isalpha(test2[i])&&isalpha(test2[i-1])){int ans=binsearch(magic,o);//printf("%s,ans:::%d\n",magic,ans);if(ans==-1)printf("%s",magic);elseprintf("%s",dic[ans].dic1);printf("%c",test2[i]);k=0;memset(magic,'\0',sizeof(magic));}else if(!isalpha(test2[i])&&!isalpha(test2[i-1])){printf("%c",test2[i]);k=0;}}printf("\n");}return 0;}
字典树方法:
#include <stdio.h>#include <malloc.h>#include <cstring>#include <ctype.h>struct tree{char sign[20];struct tree *next[26];int mark;}*root;void insert(char *b,char *a){int len=strlen(b);tree *p=root,*q;for(int i=0;i<len;i++){if(p->next[b[i]-'a']==NULL){q=(tree*)malloc(sizeof(tree));for(int j=0;j<26;j++)q->next[j]=NULL;p->next[b[i]-'a']=q;//p=q;//p=p->next[b[i]-'a'];}p=p->next[b[i]-'a'];}p->mark=1;strcpy(p->sign,a);}char *find(char *s){int len=strlen(s);tree *a=root;for(int i=0;i<len;i++){a=a->next[s[i]-'a'];if(a==NULL)return NULL;}if(a->mark==1)return a->sign;return NULL;}void clear(tree *a){if(a==NULL)return;else{for(int i=0;i<26;i++)clear(a->next[i]);}free(a);}int main(){char st[20];char a[20],b[20];char test2[3010];root=(tree*)malloc(sizeof(tree));for(int i=0;i<26;i++){root->next[i]=NULL;}scanf("%s",st);while(scanf("%s",a)&&strcmp(a,"END")){scanf("%s",b);insert(b,a);}scanf("%s",st);getchar();//printf("sdsdsd");while(gets(test2)){if(strcmp(test2,"END")==0)break;int len=strlen(test2);char magic[20];int k=0;for(int i=0;i<len;i++){while(isalpha(test2[i])){magic[k++]=test2[i];i++;}magic[k]='\0';int len=strlen(magic);if(len==0){printf("%c",test2[i]);k=0;continue;}char *ans=find(magic);if(ans==0)printf("%s",magic);elseprintf("%s",ans);printf("%c",test2[i]);k=0;}printf("\n");}clear(root);}



0 0
原创粉丝点击