poj 2503 哈希 Map 字典树
2015-08-21 11:23
381 查看
Babelfish
Description
You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.
Input
Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language
word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.
Output
Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".
Sample Input
Sample Output
Hint
Huge input and output,scanf and printf are recommended.
Source
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 36967 | Accepted: 15749 |
You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.
Input
Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language
word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.
Output
Output is the message translated to English, one word per line. Foreign 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
Sample Output
cat eh loops
Hint
Huge input and output,scanf and printf are recommended.
Source
//16096K 2625MS #include<iostream> #include<cstdio> #include<map> #include<cstring> using namespace std; int main() { char s[100],s1[11]; string ss; char c; map<string,string>Q; int num; while(gets(s)&&s[0]!='\0') //读串比读多个字符快 { int len=strlen(s); int i; for( i=0;i<len;i++) { if(s[i]==' ') {s[i]='\0'; break; } } ss=s+i+1; Q[ss]=s; } while(~scanf("%s",s1)) { if(Q[s1].size()) cout<<Q[s1]<<endl; else printf("eh\n"); } } // while(~scanf("%c",&c)) // 超时 // { // if(c=='\n') // break; // num=0; // while(c!=' ') // { // s[num++]=c; // scanf("%c",&c); // } // s[num]='\0'; //够成字符串 // num=0; // scanf("%c",&c); //防止上一个空格被读入 // // while(c!='\n') // { // ss[num++]=c; // scanf("%c",&c); // } // ss[num]='\0'; // Q[ss]=s; // }
//字典树 26240K <span id="transmark"></span>735MS #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> using namespace std; char ss[100],s[100],c[100010][100]; int num=0; struct node { int flag; node *next[26]; }*head; node * Creat() { node *p; p=new node; p->flag=0; for(int i=0;i<26;i++) p->next[i]=NULL; return p; } int Build_Tree() { node *p=head; int len=strlen(s); for(int i=0;i<len;i++) { int a=s[i]-'a'; if(!p->next[a]) { p->next[a]=Creat(); //p->next[a]->flag=num; } p=p->next[a]; } p->flag=num; } int Find(char s1[]) { int len=strlen(s1); node *p=head; for(int i=0;i<len;i++) { int a=s1[i]-'a'; if(!p->next[a]) { return 0; } p=p->next[a]; } return p->flag; } int main() { num=1; head=Creat(); while(gets(ss)&&ss[0]!='\0') { sscanf(ss,"%s %s",c[num],s); Build_Tree(); num++; } char s1[100]; while(~scanf("%s",s1)) { int flag=Find(s1); //cout<<flag; if(flag) printf("%s\n",c[flag]); else printf("eh\n"); } }
相关文章推荐
- QTreewidgetItem关键字高亮
- html5图片裁剪控件原型【含缩放,旋转,拖动功能】---1、控件设计
- miniwebx 基于webx设计思想的轻量级mvc框架
- GrailsDispatcherServlet中的doDispatch方法
- 【sql】mysql left join,right join,inner join超详细用法解析
- byte转换为十六进制时,Integer.toHexString需要&FF的原因
- linux GUN gcc编译四个过程:预处理、编译、汇编、链接
- (六)Android中使用CountDownTimer实现倒计时功能
- javascript知识点(二)
- MySql:eclipse连接
- project facets
- 超长正整数的相加
- 找工作的一点建议:
- Linux启动过程详解(inittab、rc.sysinit、rcX.d、rc.local)
- C++编译、链接涉及到的一些基本问题
- struts2 java.io.FileNotFoundException: http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd
- msvcprt.lib(MSVCP90.dll) : error LNK2005:已经在libcpmtd.lib(xmutex.obj) 中定义
- VirtualBox 四种网络连接方式
- 后台程序去download一个网站的前台代码
- 安全密码(2013中南大学研究生复试上机题)