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
原创粉丝点击