HDU 1298 T9 // 字典树,枚举,dfs
来源:互联网 发布:女装免费加盟开淘宝店 编辑:程序博客网 时间:2024/06/05 00:12
题目描述
HDU 1298 T9
解题思路
题目大意:
给出一些单词,及其出现的频率。然后再输入九宫格键盘中的数字(2~9),然后每次按下一个键,输出此刻联想到的单词(单词频率大的优先,频率相同的话,字典序小的优先)。
另外 比如 说 “hell 4”, “hello 7”, and “hellfire 3”, 则hell出现的频率不是0, 而是4+7+3 = 14!!
参考代码
//**********************************************// Author: @xmzyt1996// Date: 2015-10-25// Name: HDU 1298.cpp//**********************************************#include <cstdio>#include <cmath>#include <cctype>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>#include <string>#include <bitset>#include <vector>#include <stack>#include <queue>#include <map>using namespace std;#define clr(a, b) memset(a, b, sizeof(a))#define rep(i, a, b) for(int i = a; i < b; ++i)#define per(i, a, b) for(int i = a; i >= b; --i)#define pt(x) cout << #x << " = " << x << endl#define ps puts("debug~~")#define all(x) (x).begin(),(x).end()#define mp make_pair#define pb push_backtypedef __int64 ll;typedef pair<int, int> pii;const double pi = acos(-1.0);const double eps = 1e-6;const int inf = 0x3f3f3f3f;const int MOD = 1e9+7;const int MAX_N = 100010;struct Trie { Trie* next[26]; int p;} node[MAX_N];int num, p, maxp;char str[105];string ans, tmp;const char tel[][5] = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"}; // 九宫格对应的字母void init () { num = 0; clr (node, 0); }int getid (char c, char base) { return c - base;}void insert (char* s, int p) { // 将单词s及其频率p插入到Trie中 Trie* head = &node[0]; while (*s) { int id = getid(*s++, 'a'); if ((head->next[id]) == NULL) head->next[id] = &node[++num]; head = head->next[id]; (head->p) += p; }}void solve (Trie* head, string s, int len, int LEN) { if (len == LEN) { // 当前联想的单词长度等于输入的长度 if ((head->p) > maxp) { //频率大于之前的最大值 ans = s; // 当前联想的单词即为答案 maxp = (head->p); // 更新最大频率 } else if (((head->p) == maxp) && s < ans) ans = s; // 处理频率相同的情况 return ; } int id = getid(tmp[len], '0'); // 得到当前按下的是第几个按键 int size = strlen(tel[id]); // 这个按键中字母的个数 rep (i, 0, size) { // 遍历当前按键的所有字母 int idx = getid(tel[id][i], 'a'); if ((head->next[idx]) == NULL) continue; solve(head->next[idx], s+tel[id][i], len+1, LEN); }}int main () {#ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);#endif int T, n, kase = 1; scanf("%d", &T); while (T--) { init(); scanf("%d", &n); while (n--) { scanf("%s %d", str, &p); insert(str ,p); } printf("Scenario #%d:\n", kase++); scanf("%d", &n); while (n--) { scanf("%s", str); int len = strlen(str); tmp = ""; rep (i, 0, len-1) { tmp += str[i]; ans = "MANUALLY"; maxp = 0; solve (&node[0], "", 0, i+1); cout << ans << endl; } puts(""); } puts(""); } return 0;}
0 0
- HDU 1298 T9 // 字典树,枚举,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:T9【字典树+dfs】
- hdu-1298 T9(字典树+DFS)
- HDU 1298 T9 字典树+dfs
- HDU 1298 T9(手机输入法相关,字典树+dfs)
- hdu 1298 字典树 + DFS (模拟T9文本输入)
- hud 1298 T9(字典树 + DFS详解)
- 【字典树+DFS】 hdu1298 T9
- HDU1298 T9 字典树+DFS
- [字典树] hdu 1298 poj 1451 T9
- 用 goto + if 模拟 循环
- eclipse集成Android NDK说明
- MySQL的configure参数详解
- uva10310(SuperSale)
- Linux文件解压缩
- HDU 1298 T9 // 字典树,枚举,dfs
- 使用NSURLCache让本地数据来代替远程UIWebView请求
- Linux文件操作
- iPhone开发【二十三】常用IOS开源组件【第1篇】
- java泛型
- 利用jsoup抓取指定网页的照片【也可以修改一下抓取其他内容】
- C++ 类访问控制(public/protected/private)
- ANDROID源码
- linux文件和目录的权限与配置