程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU 1075(What Are You Talking About-Trie的插入和查找)

HDU 1075(What Are You Talking About-Trie的插入和查找)

編輯:C++入門知識

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others) Total Submission(s): 8571    Accepted Submission(s): 2696     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 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.       Author Ignatius.L     Trie的插入和查找 話說也不難啊…… [cpp]  #include<cstdio>   #include<cstring>   #include<algorithm>   #include<functional>   #include<cstdlib>   #include<iostream>   #include<cctype>   using namespace std;   #define MAXWordlen (10+10)   #define MAXLen (3000+10)   char s[MAXLen];   char word[MAXWordlen],word1[MAXWordlen];   struct node   {       node *next[26+1];       char t[MAXWordlen];       node()       {           t[0]=0;           for (int i=0;i<=26;i++) next[i]=NULL;       }   }root;   void insert(char *str,char str1[])   {       int len=strlen(str);       node *p=&root;       for (int i=0;i<len;i++)       {           if (p->next[str[i]-'a']==NULL) p->next[str[i]-'a']=new node();           p=p->next[str[i]-'a'];              }       strcpy(p->t,str1);   }   node* find(int l,int r)   {       node *p=&root;       for (int i=l;i<=r;i++)       {           if (p->next[s[i]-'a']==NULL) return 0;           p=p->next[s[i]-'a'];            }       if (strcmp(p->t,"")) return p;          return 0;   }   int main()   {       scanf("START");       while (scanf("%s",word)&&strcmp(word,"END"))       {           scanf("%s",word1);           insert(word1,word);       }       gets(word);       scanf("START");       gets(word);       while (gets(s))       {           if (!strcmp(s,"END")) break;            int len=strlen(s);           s[len+1]=0;           for (int i=0;i<len;i++)           {               if (islower(s[i]))               {                   int j=i;                   while (islower(s[j+1])) j++;                   node *p=find(i,j);                   if (p) printf("%s",p->t);                   else                   {                       for (int k=i;k<=j;k++) printf("%c",s[k]);                   }                    i=j;                                }               else printf("%c",s[i]);           }           printf("\n");       }       return 0;   }    

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved