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的更多相关文章

  1. 【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, ...

  2. 【POJ】1067 取石子游戏(博弈论)

    Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

  3. 【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$都是偶数啦~ ...

  4. 【POJ】【2104】区间第K大

    可持久化线段树 可持久化线段树是一种神奇的数据结构,它跟我们原来常用的线段树不同,它每次更新是不更改原来数据的,而是新开节点,维护它的历史版本,实现“可持久化”.(当然视情况也会有需要修改的时候) 可 ...

  5. 【POJ】1222 EXTENDED LIGHTS OUT

    [算法]高斯消元 [题解] 高斯消元经典题型:异或方程组 poj 1222 高斯消元详解 异或相当于相加后mod2 异或方程组就是把加减消元全部改为异或. 异或性质:00 11为假,01 10为真.与 ...

  6. 【POJ】2892 Tunnel Warfare

    [算法]平衡树(treap) [题解]treap知识见数据结构 在POJ把语言从G++换成C++就过了……??? #include<cstdio> #include<algorith ...

  7. 【POJ】【1637】Sightseeing tour

    网络流/最大流 愚人节快乐XD 这题是给一个混合图(既有有向边又有无向边),让你判断是否有欧拉回路…… 我们知道如果一个[连通]图中每个节点都满足[入度=出度]那么就一定有欧拉回路…… 那么每条边都可 ...

  8. 【poj】1001

    [题目] ExponentiationTime Limit: 500MS Memory Limit: 10000KTotal Submissions: 123707 Accepted: 30202De ...

  9. 【POJ】3070 Fibonacci

    [算法]矩阵快速幂 [题解] 根据f[n]=f[n-1]+f[n-2],可以构造递推矩阵: $$\begin{vmatrix}1 & 1\\ 1 & 0\end{vmatrix} \t ...

随机推荐

  1. ubuntu 下编译安装 mysql php nginx 及常见错误 (持续添加)

    mysql mysql 可以使用mysql 官方提供的apt源进行安装 参见这里 php 安装前先安装一些常见库 sudo apt-get install libpng16-16 libpng16-d ...

  2. Java Post 数据请求和接收

    这两天在做http服务端请求操作,客户端post数据到服务端后,服务端通过request.getParameter()进行请求,无法读取到数据,搜索了一下发现是因为设置为text/plain模式才导致 ...

  3. HTML+CSS基础学习笔记(3)

    一.提交按钮.重置按钮 1.type="submit" 提交按钮 2.type="reset"   重置按钮 二.form表单中的label标签 格式: < ...

  4. PS软件之,快速的修改图片你的尺寸

    进入 -- 图像 --- 图像尺寸 -- (前面两个去掉后,只剩下最后一个选项的时候就能够任意的修改图像的尺寸)

  5. H TML5 之 (5) 一个在线画图板

    这里加入了点难度了,增加了对HTML很多时间的把握,对象的把握 index.html <!DOCTYPE HTML> <html> <head> <title ...

  6. css 导航条 布局

    导航条(简单易用的导航) <!DOCTYPE html> <html> <head> <style> ul { list-style-type:none ...

  7. Java获取项目路径

    参考博客.自己就不写了.我觉得他写得很详细 http://blog.csdn.net/hpf911/article/details/5852127

  8. Java--获取request中所有参数的方法

    我们通常用到request获取某个参数的方法: String value=request.getparameter("key"); 如果想要获取request中所有的参数呢? re ...

  9. Linux C++服务器程序设计范式

    <Unix网络编程>30章详细介绍了几种服务器设计范式.总结了其中的几种,记录一下: 多进程的做法: 1.每次创建一个新的请求,fork一个子进程,处理该连接的数据传输. 2.预先派生一定 ...

  10. phpstorm集成phpunit(转)

    转自http://blog.csdn.net/zhuziying99/article/details/49028321 phpstorm集成phpunit1.下载phpunit.phar,将该文件放到 ...