• 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; }
  • 相关阅读:
    食物链(并查集)
    字母奔跑
    19+199+1999+……+1999…9(1999个9)和是多少?
    利用union判断CPU是大端模式还是小端模式
    闰年个数(非循环)
    计算catlan数f(n)(动态规划)
    模拟循环调度(队列)
    回溯表达式,+不小于4个,乘号不小于2个,列出表达式种数
    枚举{1,2,3,4}子集
    回溯n个元素的子集
  • 原文地址:https://www.cnblogs.com/372465774y/p/3010610.html
Copyright © 2020-2023  润新知