HDU 1075 字典树 树搜索 逆路径输出单词
2016-08-05 13:09
369 查看
题目链接
题意: 给出一个字典 和 文章 根据字典翻译文章 字典中没有的词不用翻译 标点不用翻译
将字典中的所有单词都Insert进一颗字典树中 MAR单词的节点的Eng指针指向它对应的ENGLISH单词节点 所有Eng指针为NULL的不是MAR单词
找到对应的ENGLISH单词节点之后 又pre指针向上直到root 沿途将字母存入temp数组中最后将数组逆序
代码:
题意: 给出一个字典 和 文章 根据字典翻译文章 字典中没有的词不用翻译 标点不用翻译
将字典中的所有单词都Insert进一颗字典树中 MAR单词的节点的Eng指针指向它对应的ENGLISH单词节点 所有Eng指针为NULL的不是MAR单词
找到对应的ENGLISH单词节点之后 又pre指针向上直到root 沿途将字母存入temp数组中最后将数组逆序
代码:
#include <cstdio> #include <cstring> #define sf scanf #define pf printf using namespace std; const int sigm_size = 26; typedef struct TrieNode* point; struct TrieNode{ char value; point Eng_point; point next[26]; point pre; }root; point make_new_node(char value,point pre){ point cur = new TrieNode; cur -> Eng_point = NULL; for(int i = 0;i < 26;++i){ cur -> next[i] = NULL; } cur -> pre = pre; cur -> value = value; return cur; } void Insert_Mar(char *s,point value){ point cur = & root; while(*s){ int idx = *s - 'a'; if(cur -> next[idx] == NULL){ cur -> next[idx] = make_new_node(*s,cur); } cur = cur -> next[idx]; s++; } cur -> Eng_point = value; return; } point Insert_Eng(char* s){ point cur = & root; while(*s){ int idx = *s - 'a'; if(cur -> next[idx] == NULL){ cur -> next[idx] = make_new_node(*s,cur); } cur = cur -> next[idx]; s++; } return cur; } void Insert_Pair(char* Eng,char* Mar){ Insert_Mar(Mar,Insert_Eng(Eng)); } point Search_Mar(char* s,int len){ // 返回对应的ENG的指针 没有返回NULL则树中没有对应的MAR point cur = &root; for(int i = 0;i < len;++i){ int idx = s[i] - 'a'; if(cur -> next[idx] == NULL) return NULL; cur = cur -> next[idx]; } return cur -> Eng_point; } void Mar_To_Eng(char* s,int len,char* temp){ point End_Eng = Search_Mar(s,len); if(End_Eng == NULL){ for(int i = 0;i < len;++i){ temp[i] = s[i]; } temp[len] = '\0'; return; } int p = 0; while(End_Eng != &root){ temp[p++] = End_Eng -> value; End_Eng = End_Eng -> pre; } temp[p] = '\0'; strrev(temp); return; } char buff[3005]; char Eng[15],Mar[15],temp[15]; int main(){ root.Eng_point = NULL; root.pre = NULL; root.value = '\0'; for(int i = 0;i < 26;++i) root.next[i] = NULL; while( gets(buff) != 0){ if(buff[0] == 'S'){ continue; } else if(buff[0] == 'E'){ break; } else{ sscanf(buff,"%s %s",Eng,Mar); Insert_Pair(Eng,Mar); } } while(gets(buff) != 0){ if(buff[0] == 'S'){ continue; } else if(buff[0] == 'E'){ break; } else{ int buff_len = strlen(buff); int pre = -1; for(int i = 0;i < buff_len;++i){ if(buff[i] >= 'a' && buff[i] <= 'z'){ if(pre == -1) pre = i; } else { if(pre != -1){ Mar_To_Eng(buff + pre,i - pre,temp); pf("%s",temp); } pf("%c",buff[i]); pre = -1; } } if(pre != -1){ Mar_To_Eng(buff + pre,buff_len - pre,temp); pf("%s",temp); } pf("\n"); } } return 0; }
相关文章推荐
- 搜狗百度360市值齐跌:搜索引擎们陷入集体焦虑?
- 本人即将筹备败家日志,敬请期待!
- IE:使用搜索助手
- C++深度优先搜索的实现方法
- 基于文本的搜索
- php实现搜索一维数组元素并删除二维数组对应元素的方法
- 使用Sphinx对索引进行搜索
- asp 多关键词搜索的简单实现方法
- C#使用foreach语句搜索数组元素的方法
- WordPress中用于获取搜索表单的PHP函数使用解析
- JavaScript中数组的排序、乱序和搜索实现代码
- jquery ztree实现树的搜索功能
- 【经典源码收藏】jQuery实用代码片段(筛选,搜索,样式,清除默认值,多选等)
- Ajax实现智能提示搜索功能
- C#编程实现Excel文档中搜索文本内容的方法及思路
- sqlserver中在指定数据库的所有表的所有列中搜索给定的值
- 可以用来搜索当前页面内容的js代码
- 全文搜索和替换
- javascript搜索自动提示功能的实现第1/3页
- iOS应用中UISearchDisplayController搜索效果的用法