http://acm.hdu.edu.cn/showproblem.php?pid=1298

题意:
模拟手机9键,给出每个单词的使用频率。现在给出按键的顺序,问每次按键后首字是什么(也就是要概率最大的)。

思路:

先建立字典树算出每个前缀出现的概率,然后dfs每种情况,选择概率大的。

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = *+; char s[],ans[],tmp[]; int n, num = , p;
int pheno[][] = {{,,},{,,},{,,},{,,},{,,},{,,,},{,,},{,,,}};
int key[] = {,,,,,,,}; struct Trie
{
int son[];
int cnt;
}t[maxn]; void init(int x)
{
t[x].cnt = ;
memset(t[x].son,,sizeof(t[x].son));
} void insert(char* s, int d)
{
int u = , n = strlen(s);
for(int i=;i<n;i++)
{
int c = s[i]-'a';
if(!t[u].son[c])
{
num++;
init(num);
t[u].son[c] = num;
}
u = t[u].son[c];
t[u].cnt += d;
}
} void dfs(int cur, int u, int len, char* s)
{
if(cur == len)
{
if(t[u].cnt > p)
{
p = t[u].cnt;
for(int i=;i<len;i++) ans[i] = tmp[i];
ans[len] = '\0';
}
return;
}
int k = s[cur]-'';
for(int i=;i<key[k];i++)
{
int c = pheno[k][i];
if(!t[u].son[c]) continue;
tmp[cur] = 'a'+ c;
dfs(cur+, t[u].son[c], len, s);
}
} int main()
{
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
int kase = ;
while(T--)
{
init();
scanf("%d",&n);
for(int i =;i<n;i++)
{
scanf("%s%d",s,&p);
insert(s,p);
}
printf("Scenario #%d:\n",++kase);
int q; scanf("%d",&q);
while(q--)
{
scanf("%s",&s);
int len = strlen(s);
for(int i=;i<len-;i++)
{
p = ;
dfs(,,i+,s);
if(p) printf("%s\n",ans);
else puts("MANUALLY");
}
if(q) puts("");
}
printf("\n\n");
}
return ;
}

HDU 1298 T9(字典树+dfs)的更多相关文章

  1. HDU 1298 T9 字典树+DFS

    必须要批评下自己了,首先就是这个题目的迟疑不定,去年做字典树的时候就碰到这个题目了,当时没什么好的想法,就暂时搁置了,其实想法应该有很多,只是居然没想到. 同样都是对单词进行建树,并插入可能值,但是拨 ...

  2. HDU 1298 T9 ( 字典树 )

    题意 : 给你 w 个单词以及他们的频率,现在给出模拟 9 键打字的一串数字,要你在其模拟打字的过程中给出不同长度的提示词,出现的提示词应当是之前频率最高的,当然提示词不需要完整的,也可以是 w 个单 ...

  3. hdu 1298 T9(特里+DFS)

    pid=1298" target="_blank" style="">题目连接:hdu 1298 T9 题目大意:模拟手机打字的猜想功能.依据概 ...

  4. hdu 1298 T9

    字典树+DFS. #include<cstdio> #include<cstring> #include<cmath> #include<string> ...

  5. hdu 1298 字典树 + DFS (模拟T9文本输入)

    题意:       给你一些按键顺序,让你输出每一步中概率最大的那个单词,这里的概率计算方 法好好看看别弄错了,一开始就是因为弄错了,各种wa,比如 abc 1 ,ab 1,那么 ab 的概率就是2 ...

  6. HDU 1298 T9【字典树增加||查询】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1298 T9 Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  7. HDU 6191 2017ACM/ICPC广西邀请赛 J Query on A Tree 可持久化01字典树+dfs序

    题意 给一颗\(n\)个节点的带点权的树,以\(1\)为根节点,\(q\)次询问,每次询问给出2个数\(u\),\(x\),求\(u\)的子树中的点上的值与\(x\)异或的值最大为多少 分析 先dfs ...

  8. hdu多校第五场1002 (hdu6625) three arrays 字典树/dfs

    题意: 给你两个序列a,b,序列c的某位是由序列a,b的此位异或得来,让你重排序列ab,找出字典序最小的序列c. 题解: 如果能找到a,b序列中完全一样的值当然最好,要是找不到,那也尽量让低位不一样. ...

  9. HDU1298 字典树+dfs

    T9 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...

随机推荐

  1. Google自动广告,将广告代码放置在 HTML 中的什么位置?

    Google自动广告,将广告代码放置在 HTML 中的什么位置? 为自动广告生成广告代码后,您需要将此代码放置在要展示广告的每个网页中.您应将广告代码放置在网页的 <head> 标记(或正 ...

  2. AtCoder Beginner Contest 087 (ABC)

    A - Buying Sweets 题目链接:https://abc087.contest.atcoder.jp/tasks/abc087_a Time limit : 2sec / Memory l ...

  3. HDU 3172 Virtual Friends (map+并查集)

    These days, you can do all sorts of things online. For example, you can use various websites to make ...

  4. CAT Caterpillar ET is really a exceptional obd2 solution

    As a excellent obd2 solutions,Heavy Duty Diagnostic CAT Caterpillar ET Diagnostic Adapter features a ...

  5. 自学Java第七周的总结

    这一周里我将看过的知识点又复习了一遍,下个星期打算将题做一遍

  6. css 初级进阶

    摘自:https://www.jianshu.com/p/dcc40ccc9841 CSS中级 Class和ID选择器 CSS初级教程中我们可以使用HTML标签选择器定义样式. 同样你也可以使用Cla ...

  7. 怎样从外网访问内网MySQL数据库?

    本地安装了一个MySQL数据库,只能在局域网内访问到,怎样从外网也能访问到本地的MySQL数据库呢?本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动MySQL数据库 默认安装的MySQL ...

  8. IP代理

    import requests # 定义爬取url地址 base_url = 'https://www.baidu.com/' # 定义代理IP地址 proxies = {'http':'http:/ ...

  9. spring总结之三(依赖注入)

    DI(重要):依赖注入(Dependency Injection).一般情况下,一个类不可能独立完成一个复杂的业务,需要多个类合作共同完成,需要在类中调用其它类的方法,就要给对象赋值,程序在执行过程中 ...

  10. Introduction To Machine Learning Self-Evaluation Test

    Preface Section 1 - Mathematical background Multivariate calculus take derivatives and integrals; de ...