【POJ】2503 Babelfish(字典树,map,指针)
一、map
输入时候的格式有点难想,还有一种想法是用gets读取,然后用sscanf分开,分别存到两个数组中去,再加入map中,但是这一种方法目前还没有实现。。
#include <iostream>
#include <cstring>
#include <string>
#include <map>
#include <algorithm>
using namespace std;int main ()
{string s1,s2;char a[1000],b[1000];char t;map<string,string> m;while(true){int i=0;t=getchar();if(t=='\n')break;else{a[i++]=t;while(true){if((t=getchar())==' '){a[i]='\0';break;}elsea[i++]=t;}scanf("%s",b);getchar();m[b]=a;}}char c[1000];map<string,string>::iterator it;while(scanf("%s",c)!=EOF){it=m.find(c);if(it!=m.end())cout << m[c] << endl;elsecout << "eh\n";}return 0;
}
二、链表
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;typedef struct node
{char *str;node *next[26];node(){str = NULL;for(int i=0;i<26;i++)next[i] = NULL;}
}N ;
N *root;void insert(char *word,char *trans)
{N *p=root;for(int i=0;word[i];i++){int x = word[i] - 'a';if(p->next[x]==NULL)p->next[x] = new N;p=p->next[x];}p->str = new char[12];strcpy(p->str,trans);
}void find(char *word)
{N *p=root;for(int i=0;word[i];i++){int x = word[i]-'a';if(p->next[x]==NULL){printf("eh\n");return ;}p=p->next[x];}if(p->str!=NULL)printf("%s\n",p->str);elseprintf("eh\n");
}void del_node(N *root)
{for(int i=0;i<26;i++){if(root->next[i]!=NULL)del_node(root->next[i]);}delete(root);
}int main ()
{char s[25],word[12],trans[12];root = new N;while(gets(s)){if(s[0]=='\0')break;sscanf(s,"%s%s",trans,word);insert(word,trans);}while(scanf("%s",s)!=EOF)find(s);del_node(root);return 0;
}
【POJ】2503 Babelfish(字典树,map,指针)相关推荐
- poj 2513(欧拉路径+字典树映射)
题目链接:http://poj.org/problem?id=2513 思路:题目还是很简单的,就是判断是否存在欧拉路径,我们给每个单词的头和尾映射序号,统计度数.对于给定的无向图,当且仅当图连通并且 ...
- CodeForces - 858D Polycarp's phone book(字典树/map)
题目链接:点击查看 题目大意:给出n个电话号码,每个电话号码都由9位数字组成,我们需要输出每个电话号码的最小关键词,最小关键词是指当输入这个关键词后,只能与当前的电话号码的其中一段匹配,而不能和其他电 ...
- 字典树-数组指针实现+数组实现
提出问题 给定一个字符串集合W,然后询问该集合中是否存在字符串S. eg:W={"acd","bcd","abcd","abcde ...
- Phone List POJ - 3630(字典树模板题)
题意 给定 n个长度不超过 10的数字串,问其中是否存在两个数字串S,T ,使得 S是 T的前缀,多组数据. 题目 Given a list of phone numbers, determine i ...
- poj - 2503 Babelfish
简单字典树. 1 #include <stdio.h> 2 #include <string.h> 3 const int N = 200000; 4 int nxt[N][2 ...
- Poj 2503 Babelfish(Map操作)
一.Description You have just moved from Waterloo to a big city. The people here speak an incomprehens ...
- [POJ 2503] Babelfish【二分查找】
题目大意 给你一个词典,每行有两个字符串x,y,表示y翻译成x. 一行空行隔开. 给你一些单词y,问你翻译后的x,如果词典里没有,那么就输出"eh" 解题报告 C++调map,还可 ...
- hdu 1251 字典树,指针版
这题内存需求很大,我用静态数组写的trie总MLE,看discuss说50w的数组才过,但我一开50w的就MLE了啊--想不通 这是链表版的trie,1个case,所以也不用考虑内存释放什么的 #in ...
- POJ - 2503
POJ - 2503 这题用map做时有2100ms,而用字典树去做时360ms就过了. map的 字典树的: 下面是代码: 1 #include<iostream> 2 #include ...
- hdu2846(2009多校第四场) 字典树
明明就是个简单的字典树,我不过就是想复习一下尼玛被自己坑死了T^T 把字符串分解那儿写错了,检查了半天没找到错误,然后以为被卡数组内存,难过的学习字典树的指针写法.. 发现还是不对,大半夜突然看到自己 ...
最新文章
- 图书管理系统python源代码-Python实现图书管理系统
- 面试题-两个数值交换
- set和vector
- 【图像处理】——改变图像的大小(降采样重采样)下采样和上采样
- docker与jenkins学习
- UOJ Test Round 3
- 用 RxSwift 实现通用的响应式转场
- python—15种字符串操作
- 培华学院2012届计算机专业多少人,西安培华学院学子在中国大学生计算机设计大赛中取得多个国奖...
- 清华计算机专业培养,清华大学计算机本科生培养方案..pdf
- Java使用数组学习心得
- SWD离线烧写器(完全开源)
- 软件测试面试题之自动化测试题大合集(上)
- 2015小米暑期实习笔试题_风口的猪-中国牛市(dp)
- JAVA新生入学报到管理系统计算机毕业设计Mybatis+系统+数据库+调试部署
- webaudio ajax,Web Audio 入门之读取左右声道数据
- 再也不用熬夜爆肝做汇报了!PPT生成神器ChatBCG来啦!
- 说话人识别的特征选取
- 180701 icon文件查找与转换网站
- html 表格自动排序,jQuery html表格排序插件tablesorter使用方法详解