HDU 1298 T9 // 字典树,枚举,dfs

题目描述

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_back
typedef __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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值