【POJ】1451 T9
DFS+字典树。
#include <cstdio>
#include <cstring>
#include <cstdlib> typedef struct Trie {
int v;
Trie *next[];
} Trie; Trie root;
int arr[] = {,,,,,,,,,,};
char buf[], tmp[];
char map[][];
int max[], len; void create(char str[], int x) {
int i, j, id;
Trie *p = &root, *q; for (i=; str[i]!='\0'; ++i) {
id = str[i] - 'a';
if (p->next[id] == NULL) {
q = (Trie *)malloc(sizeof(Trie));
q->v = x;
for (j=; j<; ++j)
q->next[j] = NULL;
p->next[id] = q;
} else {
p->next[id]->v += x;
}
p = p->next[id];
}
} void del(Trie *t) {
int i; if (t == NULL)
return ;
for (i=; i<; ++i)
del(t->next[i]);
free(t);
} void find(Trie *t, int ti, int step) {
int beg = arr[buf[step]-''];
int end = arr[buf[step]-''+];
int i; for (i=beg; i<end; ++i) {
if (t->next[i] == NULL)
continue;
tmp[ti] = i+'a';
if (t->next[i]->v > max[step]) {
max[step] = t->next[i]->v;
tmp[ti+] = '\0';
strcpy(map[step], tmp);
}
if (step < len-)
find(t->next[i], ti+, step+);
}
} int main() {
int case_n, n, x;
int i, j, t; scanf("%d", &case_n); for (t=; t<=case_n; ++t) {
for (i=; i<; ++i)
root.next[i] = NULL;
scanf("%d", &n);
while (n--) {
scanf("%s %d", buf, &x);
create(buf, x);
}
scanf("%d", &n);
printf("Scenario #%d:\n", t);
while (n--) {
scanf("%s", buf);
len = strlen(buf);
--len;
memset(max, -, sizeof(max));
find(&root, , );
for (i=; i<len; ++i) {
if (max[i] == -)
break;
printf("%s\n", map[i]);
}
for (j=i; j<len; ++j)
printf("MANUALLY\n");
printf("\n");
}
printf("\n");
del(&root);
} return ;
}
【POJ】1451 T9的更多相关文章
- 【POJ】1704 Georgia and Bob(Staircase Nim)
Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...
- 【POJ】1067 取石子游戏(博弈论)
Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...
- 【BZOJ】【1986】【USACO 2004 Dec】/【POJ】【2373】划区灌溉
DP/单调队列优化 首先不考虑奶牛的喜欢区间,dp方程当然是比较显然的:$ f[i]=min(f[k])+1,i-2*b \leq k \leq i-2*a $ 当然这里的$i$和$k$都是偶数啦~ ...
- 【POJ】【2104】区间第K大
可持久化线段树 可持久化线段树是一种神奇的数据结构,它跟我们原来常用的线段树不同,它每次更新是不更改原来数据的,而是新开节点,维护它的历史版本,实现“可持久化”.(当然视情况也会有需要修改的时候) 可 ...
- 【POJ】1222 EXTENDED LIGHTS OUT
[算法]高斯消元 [题解] 高斯消元经典题型:异或方程组 poj 1222 高斯消元详解 异或相当于相加后mod2 异或方程组就是把加减消元全部改为异或. 异或性质:00 11为假,01 10为真.与 ...
- 【POJ】2892 Tunnel Warfare
[算法]平衡树(treap) [题解]treap知识见数据结构 在POJ把语言从G++换成C++就过了……??? #include<cstdio> #include<algorith ...
- 【POJ】【1637】Sightseeing tour
网络流/最大流 愚人节快乐XD 这题是给一个混合图(既有有向边又有无向边),让你判断是否有欧拉回路…… 我们知道如果一个[连通]图中每个节点都满足[入度=出度]那么就一定有欧拉回路…… 那么每条边都可 ...
- 【poj】1001
[题目] ExponentiationTime Limit: 500MS Memory Limit: 10000KTotal Submissions: 123707 Accepted: 30202De ...
- 【POJ】3070 Fibonacci
[算法]矩阵快速幂 [题解] 根据f[n]=f[n-1]+f[n-2],可以构造递推矩阵: $$\begin{vmatrix}1 & 1\\ 1 & 0\end{vmatrix} \t ...
随机推荐
- System Operations on AWS - Lab 3W - Managing Storage (Windows)
创建一个名叫Processor的EC2实例,登陆到CommandHost实例,通过AWS CLI对Processor实例的EBS卷做snapshot,设置周期性snapshot的计划任务, 登陆到Pr ...
- Migration of ASP.NET app from IIS6 to IIS7 (7.5)
For many of us familiar problem. You developing applications under IIS6 and you're about to move the ...
- Windows Server 2008 R2 安装及配置指南
一.安装需求: 1. 硬件需求条件 硬件 需求 处理器 最低:1.4 GHz(x64处理器) 注意:Windows Server 2008 for Itanium-Based Systems 版本需要 ...
- 第二篇:web之前端之css
前端之css 前端之css 本节内容 css概述及引入 css选择器 css常用属性 1.css概述及引入 CSS概述 CSS是Cascading Style Sheets的简称,中文称为层叠样式 ...
- 安卓Intent(隐式)
相对于显式Intent(明确指出了目标组件名称的Intent),隐式Intent,没有明确指出目标组件名称.显式Intent直接用组件的名称定义目标组件,这种方式很直接.但是由于开发人员往往并不清楚别 ...
- 详解android:scaleType属性
详解android:scaleType属性 转自:http://blog.csdn.net/encienqi/article/details/7913262 http://juliaailse. ...
- AIX filesystemcache引发的Oracle事故
链接地址: http://www.jydba.net/aix-filesystemcache%e5%bc%95%e5%8f%91%e7%9a%84oracle%e4%ba%8b%e6%95%85/ A ...
- C# 之【线程与进程】
1. 引言 先来个比喻手法: 如果把上课的过程比作进程,那么每个学生就是一个线程,他们共享教室,即线程共享进程的内存空间.每一个时刻,只能一个学生问老师问题,老师回答完毕,轮到下一个.即线程在一个时 ...
- CSS 尺寸 (Dimension)
CSS 尺寸 (Dimension) 属性允许你控制元素的高度和宽度.同样,它允许你增加行间距. 更多实例 设置元素的高度 这个例子演示了如何设置不同元素的高度. 使用百分比设置图像的高度 这个例子演 ...
- Java OOP考试错题分析
解析: A.ArrayList 可以存储NULL值,也可以存储重复的值,对集合没有任何影响. B.一旦实例化不可改变自身大小,这是数组的特性.集合的容量是自身扩容的. C.ArrayList可以存 ...