HDU 1298 T9 ( 字典树 )
题意 : 给你 w 个单词以及他们的频率,现在给出模拟 9 键打字的一串数字,要你在其模拟打字的过程中给出不同长度的提示词,出现的提示词应当是之前频率最高的,当然提示词不需要完整的,也可以是 w 个单词出现最多次数的前缀。
分析 : 利用字典树存储这 w 个单词,然后给每一个结点的权值赋上其单词出现频率,接下来的工作就简单多了,只要暴力枚举模拟输入的数字键盘可能产生的单词组合,然后保存频率最高的那个即可!
#include<string.h>
#include<stdio.h>
#include<malloc.h>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
;
, n, m;
const int INF = 0x3f3f3f3f;
]{ ///模拟数字键盘,因为是从2开始,所以0和1都赋值上一个'@'代表空
{'@','@','@','@','@'},
{'@','@','@','@','@'},
{'a','b','c','@','@'},
{'d','e','f','@','@'},
{'g','h','i','@','@'},
{'j','k','l','@','@'},
{'m','n','o','@','@'},
{'p','q','r','s','@'},
{'t','u','v','@','@'},
{'w','x','y','z','@'}
};
struct Trie
{
Trie *Next[maxn];
int v;
inline void init(const int val){
this->v = val;
; i<maxn; i++)
this->Next[i] = NULL;
}
};
Trie *root;
void CreateTrie(char *str, int val)
{
int len = strlen(str);
Trie *p = root, *tmp;
; i<len; i++){
int idx = str[i]-'a';
if(p->Next[idx] == NULL){
tmp = (Trie *)malloc(sizeof(Trie));
tmp->init(val);
p->Next[idx] = tmp;
}else (p->Next[idx]->v) += val;///赋上频率权值
p = p->Next[idx];
}
}
inline void DelTrie(Trie *T)
{
if(T == NULL) return ;
; i<maxn; i++){
if(T->Next[i] != NULL)
DelTrie(T->Next[i]);
}
free(T);
return ;
}
], ans[], now[];
int tmp, MaxVal;
void DFS(int pos, int len, Trie *NOD)
{
if(pos == len){///递归出口,看看频率是否更优,更新答案
if(NOD->v > MaxVal){
strcpy(ans, now);
MaxVal = NOD->v;
}
return ;
}
';
; mp[idx][i]!='@'; i++){///枚举这个数字建的所有字母,遇到'@'结束
int id = mp[idx][i] - 'a';
if(NOD->Next[id]){ ///如果字典树关于这个字母的节点不为空的话,则进一步搜索
now[pos] = mp[idx][i];///now是保存一下暂时现在构造出来的字符串,以便更新
now[pos+] = '\0';
DFS(pos+, len, NOD->Next[id]);///进一步搜索
}
}
}
int main(void)
{
int nCase;
scanf("%d", &nCase);
; Case<=nCase; Case++){
root = (Trie *)malloc(sizeof(Trie));
root->init();
scanf("%d", &n);
int tmpVal;
; i<n; i++){
scanf("%s %d", str, &tmpVal);
CreateTrie(str, tmpVal);
}
printf("Scenario #%d:\n", Case);
scanf("%d", &m);
; i<m; i++){
scanf("%s", str);
int len = strlen(str);
; j<len; j++){///枚举长度
MaxVal = -INF;
DFS(, j, root);
if(MaxVal == -INF) puts("MANUALLY");
else puts(ans);
}
puts("");
}puts("");
DelTrie(root);
}
;
}
HDU 1298 T9 ( 字典树 )的更多相关文章
- HDU 1298 T9 字典树+DFS
必须要批评下自己了,首先就是这个题目的迟疑不定,去年做字典树的时候就碰到这个题目了,当时没什么好的想法,就暂时搁置了,其实想法应该有很多,只是居然没想到. 同样都是对单词进行建树,并插入可能值,但是拨 ...
- hdu 1298 T9(特里+DFS)
pid=1298" target="_blank" style="">题目连接:hdu 1298 T9 题目大意:模拟手机打字的猜想功能.依据概 ...
- HDU 1298 T9【字典树增加||查询】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1298 T9 Time Limit: 2000/1000 MS (Java/Others) Memo ...
- HDU 1298 T9(字典树+dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1298 题意:模拟手机9键,给出每个单词的使用频率.现在给出按键的顺序,问每次按键后首字是什么(也就是要概率最大的 ...
- POJ 1451 - T9 - [字典树]
题目链接:http://bailian.openjudge.cn/practice/1451/ 总时间限制: 1000ms 内存限制: 65536kB 描述 Background A while ag ...
- hdu 1979 DFS + 字典树剪枝
http://acm.hdu.edu.cn/showproblem.php?pid=1979 Fill the blanks Time Limit: 3000/1000 MS (Java/Others ...
- hdu 2846(字典树)
Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- HDU 2846 Repository (字典树 后缀建树)
Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total ...
- HDU 1671 (字典树统计是否有前缀)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1671 Problem Description Given a list of phone number ...
随机推荐
- OutLook会议室预定提醒
项目组采用敏捷开发管理,每两周一个迭代.写个工具做会议室预定. 代码下载:https://download.csdn.net/download/linmilove/10547579 Appointme ...
- (四) appium-desktop 脚本录制常用AW使用介绍
通过使用appium-desktop录制脚本,编写app自动化脚本的过程中,会使用到一些AW,下面就这些AW的使用方法做详细的介绍.通过实践可以看到这几个AW可以完成测试工作. AWOpenGiveP ...
- sklearn+nltk ——情感分析(积极、消极)
转载:https://www.iteye.com/blog/dengkane-2406703 步骤: 1 有标签的数据.数据:好评文本:pos_text.txt 差评文本:neg_text.txt ...
- notepad++通过调用cmd运行java程序
notepad++运行java程序方法主要有下面两个: 通过插件NppExec运行(自行百度“notepad++运行java”) 通过运行 调用cmd编译执行java程序(下面详细讲解) 点击上面工具 ...
- 用yum快速搭建LAMP平台与虚拟域名配置
实验环境: [root@nmserver-7 html]# cat /etc/redhat-release CentOS release 7.3.1611 (AltArch) [root@nmserv ...
- 最大连续和 Medium
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous s ...
- TCP滑动窗体
TCP的滑动窗体攻克了端到端的流量控制问题,同意接受方对传输进行限制.直到它拥有足够的缓冲空间来容纳很多其他的数据.滑动窗体的大小由接收方确定,接收方在发送确认信号给发送方的同一时候告诉发送方自己的缓 ...
- iOS手势操作,拖动,轻击,捏合,旋转,长按,自定义(http://www.cnblogs.com/huangjianwu/p/4675648.html)
1.UIGestureRecognizer 介绍 手势识别在 iOS 中非常重要,他极大地提高了移动设备的使用便捷性. iOS 系统在 3.2 以后,他提供了一些常用的手势(UIGestureReco ...
- Django中常用字段
一.Django框架的常用字段 Django ORM 常用字段和参数 常用字段 常用字段 AutoField int自增列,必须填入参数 primary_key=True.当model中如果没有自增列 ...
- CentOS上安装Git及配置远程仓库
首先登陆CentOS服务器,连接上服务器之后我们使用yum remove git 命令删除已安装的Git,若之前没安装过Git则不需要这一步.注意前提是你的CentOS服务器上安装了yum,这是Cen ...