zoukankan      html  css  js  c++  java
  • hdu 1075 What Are You Talking About Trie树

    What Are You Talking About

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


    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树、、结果就是悲剧的开始、、、
    //题目还是比较容易的、、树 然后查找、

    #include <stdio.h> #include <string.h> #include <algorithm> #include <vector> #include <ctype.h> #include <stack> #include <iostream> #define Max 500010 #define sigma_size 26 using namespace std; char rc[Max][14]; int cnt; char str[3002]; int idx(char c) { return c-'a'; } struct Trie { Trie *ch[sigma_size]; int point; //单词节点信息 Trie(){ memset(ch,0,sizeof(ch)); point=NULL; } }; Trie *root; void insert(char *s,int id) { Trie *t=root; int i,n=strlen(s); int c; for(i=0;i<n;i++) { c=idx(s[i]); if(!t->ch[c]) { t->ch[c]=new Trie; } t=t->ch[c]; } t->point=id; } int search(char *s) { Trie *t=root; int i,n=strlen(s); int c; for(i=0;i<n;i++) { c=idx(s[i]); if(!t->ch[c]) return 0; t=t->ch[c]; } if(t->point) return t->point; return 0; } int main() { // freopen("in.txt","r",stdin); root=new Trie; char c[14],len[14]; int id; id=1; scanf("%s",c); while(scanf("%s",rc[id]),strcmp(rc[id],"END")!=0) { scanf("%s",c); insert(c,id); id++; } getchar(); gets(str); int i,l; while(gets(str),strcmp(str,"END")!=0) { for(i=0;str[i]!='\0';i++) if(isalpha(str[i])) { l=1;len[0]=str[i]; for(i++;isalpha(str[i]);i++) len[l++]=str[i]; len[l]='\0'; id=search(len); if(id) printf("%s",rc[id]); else printf("%s",len); if(str[i]=='\0') break; printf("%c",str[i]); } else printf("%c",str[i]); printf("\n"); } return 0; }
  • 相关阅读:
    MSSQL2005和Access在SQL的某一种写法上的区别。update的一种写法不一致。
    博客园 记录 了解多一点
    马克斯4.0 采集规则的编写
    谷歌代码托管 GoogleCode中 关于 版本的一个写法
    晒晒名企大公司的工资收入
    Asp.net中DataBinder.Eval用法的总结
    Mastering Debugging in Visual Studio 2010 A Beginner's Guide
    Solution Configuration but not Platform in VS2010 Toolbar
    window.showdialog完全手册,解决模态窗口,传值和返回值问题
    从此不再惧怕URI编码:JavaScript及C# URI编码详解
  • 原文地址:https://www.cnblogs.com/372465774y/p/3010610.html
Copyright © 2011-2022 走看看