HDU 1298(字典树+dfs)
来源:互联网 发布:行之愈笃 则知之益明 编辑:程序博客网 时间:2024/06/14 06:29
题意:给一个T,表示输入数据的组数。给一个n,表示字典的大小。接下来有n行,每行有一个字符串和一个数字,数字表示为这个字符串的权值。接下来给一个m,表示手机按键的串号,结尾1表示当前输入结束,进行下一个字符。输出每一步按键应该出现的字符串。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;struct Trie{ int count; Trie* next[26]; Trie() : count(0) { memset(next, NULL, sizeof (next)); }};int _max;const int phone[8][4] = { {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11}, {12, 13, 14}, {15, 16, 17, 18}, {19, 20, 21}, {22, 23, 24, 25} }; const int num[8] = { 3, 3, 3, 3, 3, 4, 3, 4 }; void insertTrie(Trie* root, char* s, int p){ Trie* t = root; int i = 0, n; while (s[i]) { n = s[i++] - 'a'; if (t->next[n] == NULL) t->next[n] = new Trie; t = t->next[n]; t->count += p; }}void freeTrie(Trie* root){ if (root) for (int i = 0; i < 26; ++i) if (root->next[i]) freeTrie(root->next[i]); delete root;}void DFS(int cur, int len, Trie* p, char* s, char* find, char* ans){ if (cur == len) { if (p->count > _max) { _max = p->count; for (int i = 0; i < len; ++i) find[i] = ans[i]; find[len] = '\0'; } return; } int n = s[cur] - '2'; for (int i = 0; i < num[n]; ++i) { int r = phone[n][i]; if (p->next[r] == NULL) continue; ans[cur] = 'a' + r; DFS(cur + 1, len, p->next[r], s, find, ans); }}int main(){ char s[105], ans[105], find[105]; int t, n, p, m; Trie* root; scanf("%d", &t); for (int _t = 1; _t <= t; ++_t) { printf("Scenario #%d:\n",_t); scanf("%d", &n); root = new Trie; while (n--) { scanf("%s %d", s, &p); insertTrie(root, s, p); } scanf("%d", &m); while (m--) { scanf("%s", s); for (int i = 1; s[i-1] != '1'; ++i) { _max = 0; DFS(0, i, root, s, find, ans); if (_max > 0) printf("%s\n", find); else printf("MANUALLY\n"); } printf("\n"); } printf("\n"); freeTrie(root); } return 0;}
0 0
- HDU 1298(字典树+dfs)
- hdu-1298 T9(字典树+DFS)
- HDU 1298 字典树+DFS
- HDU 1298 T9 字典树+dfs
- 【字典树+DFS】hdu 1298 T9
- 字典树+DFS T9 HDU 1298
- hdu 1298 T9 字典树 dfs
- hdu 1298 T9(字典树+DFS)
- HDU 1298 T9 字典树+DFS
- [字典树 + dfs] HDU 1298 - T9
- hdu 1298 T9 字典树+DFS
- hdu-(1298手机相关 dfs+字典树)
- HDU 1298 T9 // 字典树,枚举,dfs
- HDU 1298:T9【字典树+dfs】
- HDU 1298 T9 字典树+dfs
- HDU 4776 DFS + 字典树
- HDU 1298 T9(手机输入法相关,字典树+dfs)
- hdu 1298 字典树 + DFS (模拟T9文本输入)
- C++ floor函数
- 6.SQL Server DML触发器--INSTEAD OF触发器
- MYsql discuz 未定义操作
- 关于Unity中joint的简单理解
- 市委组织部考核项目——将Excel中的每一行数据拆成多条并依次保存到数据库中
- HDU 1298(字典树+dfs)
- Python语法与类C语言语法的差别
- 【工具类】日期Calendar工具类
- 很简单的ASP,ASP.Net,PHP生成验证码
- 使用memcache实现session共享
- Android豆瓣图书查询Demo
- vc++ 不同对话框中传递信息的方法(基于自定义消息SendMessage)
- 输出排列好的星号符
- 引用