zoj 1109 Language of FatMouse(字典树)
2015-07-16 11:56
435 查看
Language of FatMouse
Time Limit: 10 Seconds
Memory Limit: 32768 KB
We all know that FatMouse doesn't speak English. But now he has to beprepared since our nation will join WTO soon. Thanks to Turing we havecomputers to help him.
Input Specification
Input consists of up to 100,005 dictionary entries, followed by a blankline, followed by a message of up to 100,005 words. Each dictionaryentry is a line containing an English word, followed by a space and a FatMouse word.No FatMouse word appears more than
once in thedictionary. The message is a sequence of words in the language of FatMouse,one word on each line. Each word in the input is a sequence of at most 10lowercase letters.
Output Specification
Output is the message translated to English, one word per line. FatMouse words not in the dictionary should be translated as "eh".
Time Limit: 10 Seconds
Memory Limit: 32768 KB
We all know that FatMouse doesn't speak English. But now he has to beprepared since our nation will join WTO soon. Thanks to Turing we havecomputers to help him.
Input Specification
Input consists of up to 100,005 dictionary entries, followed by a blankline, followed by a message of up to 100,005 words. Each dictionaryentry is a line containing an English word, followed by a space and a FatMouse word.No FatMouse word appears more than
once in thedictionary. The message is a sequence of words in the language of FatMouse,one word on each line. Each word in the input is a sequence of at most 10lowercase letters.
Output Specification
Output is the message translated to English, one word per line. FatMouse words not in the dictionary should be translated as "eh".
Sample Input
dog ogday cat atcay pig igpay froot ootfray loops oopslay atcay ittenkay oopslay
Output for Sample Input
cat eh loops 题解:建科字典树,单词插入的结尾记录该单词对应的单词的下标,查询即可。#include<cstring> #include<algorithm> #include<cstdio> #include<iostream> #include<cstdlib> #define N 100020 using namespace std; char s[12]; char ans [12]; struct Trie { int id; struct Trie *nxt[26]; Trie() { id=0; for(int i=0; i<26; i++) { nxt[i]=NULL; } } }; void Trie_Inser(Trie *p,char s[],int id) { int i=0; Trie *q=p; while(s[i]) { int nx=s[i]-'a'; if(q->nxt[nx]==NULL) { q->nxt[nx]=new Trie; } i++; q=q->nxt[nx]; } q->id=id; } int Trie_Serch(Trie *p,char s[]) { Trie *q=p; int i=0; while(s[i]) { int nx=s[i]-'a'; if(q->nxt[nx]==NULL)return 0; q=q->nxt[nx]; i++; } return q->id; } int main() { //freopen("test.in","r",stdin); Trie *p=new Trie; int id=1; char a[40]; while(1) { gets(a); if(a[0]=='\0')break; int l=0; int len=strlen(a); int i; for(i=0; i<len; i++) { if(a[i]==' ')break; ans[id][l++]=a[i]; } ans[id][l]='\0'; l=0; i++; for(; i<len; i++) { s[l++]=a[i]; } s[l]='\0'; //printf("%s %s\n",ans[id],s); Trie_Inser(p,s,id); id++; } while(~scanf("%s",s)) { id=Trie_Serch(p,s); if(id==0) { printf("eh\n"); } else { printf("%s\n",ans[id]); } } return 0; }
相关文章推荐
- 黑马程序员--Java基础
- jquery css3动态背景用户登录界面特效
- jstl jsp long to date
- SQL Server 2012大数据导入Oracle的解决方案
- css常用代码
- linux的idr机制
- Error pulling origin: error: The following untracked working tree files would be overwritten by
- Android使用Ant自动编译签名打包详解
- sql查询 所有被锁定的表
- iOS 使用MBProgressHUD 完成提示框的编写
- Unity 接入ShareSDK Android&iOS 微信分享
- javaweb之Tomcat
- 启动tomcat提示内存不足
- C#中的var类型
- .NET破解之轻量万能自定义信息管理系统
- Linux虚拟机ftp客户端与Windows ftp服务器建立连接的方法
- java AJAX实现级联下拉框
- CArray
- javascript的BOM汇总
- 关于PATHINFO的重写规则解析