【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 ...
随机推荐
- VS2010调试多进程--医疗His调试中使用
有些时候碰到自己开发的程序嵌入到别人的框架中,而在接口的地方出了问题,而又不方便将自己的模快加入到别人的工程中这个时候VS的附加到进程调试变得不可或缺了,直奔主题 http://msdn.micros ...
- C#选择文件、选择文件夹、打开文件(或者文件夹)
1.选择文件用OpenDialog OpenFileDialog dialog = new OpenFileDialog(); dialog.Multiselect = true;//该值确定是否可以 ...
- HTML+CSS基础学习笔记(5)
一.文字排版--字体 Eg: body{font-family:"宋体";} body{font-family:"Microsoft Yahei";}//微软雅 ...
- JS 自定义回调函数callback
1 应用场景:js的异步加载,在get,post,ajax异步加载的时候,可能对应的请求没有完成,这时需要使用请求回来的数据作为参数调用其他函数,这时就需要使用回调函数. 2 回调函数作用:等待函数调 ...
- 关于AVD不能导入文件的解决方案
安卓虚拟机导入文件时报以下异常: [2013-01-23 16:09:18 - ddms] transfer error: Read-only file system [2013-01-23 16:0 ...
- Android本地JUnit Text
Android本地JUnit Text 步骤 创建一个和source文件,里面添加和src目录相同的包. 在AndroidManifest.xml文件manifest根节点添加如下文件 <ins ...
- js - 在拼接字符串中动态submit当前form
今天在做一个项目的时候, mapabc中的inforWindow中,如果是超链接a,不直接响应. 后来的解决方案是动态产生form,并调用summit方法.如下 自定义一个js函数: function ...
- Ajax结合Js操作灵活操作表格
Table页面: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head& ...
- while loading persisted sessions 异常解决方法
一直用tomcat一段时间都正常无事,最近一次启动tomcat就发生以下异常: 严重: IOException while loading persisted sessions: java.io.EO ...
- 5 DML语言
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...