hdoj--1075 What Are You Talking About(Trie or map)
2016-04-12 21:04
441 查看
hdoj 1075
题解
map,或者,字典树。
#include <iostream> #include <map> #include <string> #include <cctype> #include <cstdio> #include <cstring> using namespace std; //889MS 73424K map<string, string> mp; int main() { #ifdef LOCAL freopen("data.in", "r", stdin); #endif // LOCAL ios::sync_with_stdio(0); cin.tie(0); string s, e; cin >> s; while(cin >> s && s != "END"){ cin >> e; mp[e] = s; } cin >> s; cin.ignore(); while(getline(cin, s) && s != "END"){ string tmp; for(int i = 0; i < (int)s.length(); ++i){ if(islower(s[i])){ tmp += s[i]; continue; } cout << (mp.count(tmp) ? mp[tmp] : tmp); tmp = ""; cout << s[i]; } cout << endl; } return 0; }
Trie
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> using namespace std; //g++ 468MS 128516K //c++ 686MS 65164K struct Trie { Trie* next[26]; string word; Trie():word(""){ memset(next, 0, sizeof(next)); } }; void Insert(Trie* root, string& s, string& e) { Trie* p = root; for(int i = 0; i < (int)e.length(); ++i) { int id = e[i] - 'a'; if(p->next[id] == NULL) p->next[id] = new Trie; p = p->next[id]; } p->word = s; } string Search(Trie* root, string& s) { Trie* p = root; int i; for(i = 0; i < (int)s.length(); ++i) { int id = s[i] - 'a'; p = p->next[id]; if(!p) return ""; } return p->word; } int main() { #ifdef LOCAL freopen("data.in", "r", stdin); #endif // LOCAL ios::sync_with_stdio(0); cin.tie(0); Trie* root = new Trie; string s, e; cin >> s; while(cin >> s && s != "END"){ cin >> e; Insert(root, s, e); } cin >> s; cin.ignore(); while(getline(cin, s) && s != "END"){ string tmp; for(int i = 0; i < (int)s.length(); ++i){ if(islower(s[i])){ tmp += s[i]; continue; } string res = Search(root, tmp); cout << (res != "" ? res : tmp); tmp = ""; cout << s[i]; } cout << endl; } return 0; }
相关文章推荐
- android Google Map获取地理位置信息的方法
- Spark RDD API详解(一) Map和Reduce
- Python中map()函数浅析
- Android使用Google Map浅谈
- Erlang中的映射组Map详细介绍
- c++中map的基本用法和嵌套用法实例分析
- 百度地图给map添加右键菜单(判断是否为marker)
- JavaScript中实现Map的示例代码
- jquery中map函数与each函数的区别实例介绍
- jquery中map函数遍历数组用法实例
- jquery与google map api结合使用 控件,监听器
- jquery遍历数组与筛选数组的方法
- JavaScript中实现map功能代码分享
- 用json方式实现在 js 中建立一个map
- Android Map新用法:MapFragment应用介绍
- js Map List 遍历使用示例
- java实现遍历Map的方法
- velocity显示List与Map的方法详细解析
- Google Map Api和GOOGLE Search Api整合实现代码
- Python Trie树实现字典排序