POJ 2503 Babelfish(字典树水题)
来源:互联网 发布:php微信服务号开发教程 编辑:程序博客网 时间:2024/04/29 00:41
题意:
输入一个字典,字典格式为“英语 外语”的一一映射关系
然后输入若干个外语单词,输出他们的 英语翻译单词,如果字典中不存在这个单词,则输出“eh”
解析:
对于所有给定的单词建立一颗字典树,然后对于每次给出的单词用字典树进行查询。
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;struct Node { Node* child[26]; char word[15]; bool isWord; Node() { for(int i = 0; i < 26; i++) child[i] = NULL; isWord = false; }};char noWord[] = "eh";inline int idx(char c) { return c - 'a';}void insert(Node* node, char* key, char* value) { while(*key) { int id = idx(*key); if(node->child[id] == NULL) node->child[id] = new Node(); node = node->child[id]; key++; } node->isWord = true; strcpy(node->word, value);}char* search(Node* node, char* key) { while(*key) { int id = idx(*key); if(node->child[id] == NULL) return noWord; node = node->child[id]; key++; } if(node->isWord) return node->word; return noWord;}int main() { char buf[40], key[20], value[20]; Node* root = new Node(); while(gets(buf) && buf[0] != '\0') { sscanf(buf, "%s%s", value, key); insert(root, key, value); } while(scanf("%s", key) != EOF) { printf("%s\n", search(root, key)); } return 0;}
0 0
- POJ 2503 Babelfish(字典树水题)
- POJ 2503 Babelfish(字典树)
- poj 2503 Babelfish(字典树)
- POJ 2503 Babelfish(人造字典map)
- POJ - 2503 - Babelfish(字典树)
- POJ 2503:Babelfish(字典树入门)
- poj 2503 Babelfish(字典树)
- poj 2503 Babelfish 字典树
- poj 2503 Babelfish(字典树)
- POJ 2503 Babelfish(字典树)
- poj-2503-Babelfish-字典树
- poj 2503 Babelfish(字典树)
- POJ 2503 - Babelfish(字典树)
- poj 2503 Babelfish 字典树
- POJ 2503-Babelfish【字典树】
- poj 2503 Babelfish(hash or 字典树)
- 文章标题 POJ 2503 : Babelfish(字典树)
- POJ 2503 Babelfish(STL、二分、字典树、哈希)
- 多个Controller之间的跳转
- HDU 5339-Untitled(dfs)
- org.springframework.orm.hibernate3.HibernateQueryException: unexpected token: where near line 1, col
- 深入理解java虚拟机-3 内存分配与回收策略
- unity 发布flash版本 调试需要flash player debug
- POJ 2503 Babelfish(字典树水题)
- cocos2dx游戏资源加密之XXTEA
- 视频聊天插件:AnyChat使用攻略之iOS开发指南
- 如何给Ubuntu系统清理垃圾
- 括号配对问题
- CANVAS 贝塞尔曲线
- 可视化web日志分析工具Logstalgia
- Linux-Red Hat Enterprise 6.0
- tcp ip协议笔记(3)——网络层